如何制作具有今天日期的文件夹,然后将文件复制到该文件夹? [英] How do I make a folder with today's date and then copy a file to it?
问题描述
我试图使我的一天自动化,其中一部分是创建一个日常工作文件夹并复制一个我使用的作业笔记"文件,为每个作业重命名并保存,然后在第二天再次重复.
I am trying to automate my day, part of which is creating a daily work folder and copying a 'Job Notes' file which I use, rename and save per job, repeat again the next day.
我找到了一个脚本,该脚本可以根据今天的日期创建文件夹,该脚本有效,而且我显然找到了有关如何复制文件的示例.我正在寻找的是将两者结合起来.我只需要一些基本的东西.这是我用于创建文件夹的代码.我只是不知道要在复制文件"操作中添加什么以使其使用该特定目录名(基于今天的日期).
I found a script to create a folder based on today's date, which works, and I've obviously found examples of how to copy a file. What I'm looking for is to combine the two. I just need something basic. Here is the code I have for the folder creation. I just don't know what to add to the "copy file" action to make it use that particular directory name (based on today's date).
$location = New-Item -Path C:\Users\XXXXXXX\Documents\test -ItemType Directory -Name ("$(Get-Date -f MM_dd_yy)")
$dirname = "$((get-date).toString('MM-dd'))"
md $dirname
任何帮助将不胜感激,包括以上代码不是实现此目的的最简单方法.
Any help would be greatly appreciated, including if the above code is not the easiest way to accomplish this.
P.S.无论是批处理文件还是执行此操作的PowerShell都可以,我正在研究这两种方法.
P.S. Whether a batch file or PowerShell for doing this would be fine, I am playing around learning both methods.
推荐答案
您已经在 $ location
变量中找到了位置.只需将其用作 Copy-Item
的目的地:
You already have the location in your $location
variable. Simply use that as the destination for Copy-Item
:
$basedir = "$env:USERPROFILE\Documents\test"
$today = (Get-Date).ToString('MM_dd_yy')
$location = New-Item -Path $basedir -Type Directory -Name $today
Copy-Item 'C:\path\to\job_notes.txt' -Destination $location
md
只是一个简写(使用所需参数调用 New-Item
的函数的别名).
md
is just a shorthand (an alias for a function that calls New-Item
with the required parameters).
这篇关于如何制作具有今天日期的文件夹,然后将文件复制到该文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!