如何使用Powershell中当前日期的名称创建文件夹? [英] How to create a folder using a name based on the current date in Powershell?
问题描述
例如:有3个xml文件
file_1.xml,file_2.xml和file_3.xml
现在我想要一个文件夹以格式创建 -
** xml_yyyymmdd_hhmmss **
将会放置其中的所有xml文件。
例如:Xml_20121029_180912
将是今天日期创建的文件夹。所有3个xml文件将被存储在今天。
对于明天,文件夹名称将是:
Xml_20121030_170912
我的代码如下所示:
$ location = New-Item -Path。 -ItemType Directory -Name(XML _ $(Get-Date -f dd_MM_yyyy_hhmmss))
$ rptdir =C:\Test
$ rptdir =($ rptdir +'\'+ $ location.Name)
$ outputFile =$ rptdir\File_2.xml
$ row =\\\shared\Data\DevSB\CS\AppSomeSystem.dll
& / f:$ row / o:$ outputFile
输出错误:可以没有找到路径的一部分C:\test\XML_29_10_2012_091717\File2.xml。
问题这里是 - 文件夹XML_29_10_2012_091717是使用File2.xml创建的,但不在C:\Test内,而是在脚本中。
我需要在C中创建XML_29_10_2012_091717 :\test with File2.xml inside it。
环境:Win Xp Professional。
任何帮助将不胜感激。
谢谢
尝试这样:
新项目-Path。-ItemType目录-Name(XML _ $ -Date -f ddMMyyyy_hhmmss))
评论后编辑:
尝试更改:
$ location = New-Item -Path c:\test -ItemType目录-Name(XML _ $(Get-Date - f dd_MM_yyyy_hhmmss))
$ outputFile =$($ location.fullname)\File_2.xml
I have around 50 xml files that are newly generated everytime I run a particular logic. Now I want these 50 files to be stored inside a particular date-time folder. No matter how many times ever I run that logic for one particular date, the xml files should be overwritten for that particular date only (based on the hhmmss). In simple , How to create a folder using a name based on the current date and store the xml files in them depending on the date?
For Eg: there are 3 xml files file_1.xml, file_2.xml and file_3.xml
Now I want a folder to be created in the format-
**xml_yyyymmdd_hhmmss**
that would house all the xml files in them.
For Eg: Xml_20121029_180912
would be the folder created for today's date. And all the 3 xml files will be stored in this for today.
For tomorrow the folder name would be:
Xml_20121030_170912
My code looks like below:
$location = New-Item -Path . -ItemType Directory -Name ("XML_$(Get-Date -f dd_MM_yyyy_hhmmss)")
$rptdir = "C:\Test"
$ rptdir = ($rptdir + '\' + $location.Name)
$outputFile= "$rptdir\File_2.xml"
$row = "\\shared\Data\DevSB\CS\appSomeSystem.dll"
& /f:$row /o:$outputFile
Output Error: Could not find part of the path "C:\test\XML_29_10_2012_091717\File2.xml.
The issue here is- The folder XML_29_10_2012_091717 is created with File2.xml in it but not inside the C:\Test but where the script is.
I need XML_29_10_2012_091717 to be created in C:\test with File2.xml inside it.
Environment: Win Xp Professional.
Any help would be greatly appreciated.
Thanks
Try this:
New-Item -Path . -ItemType Directory -Name ("XML_$(Get-Date -f ddMMyyyy_hhmmss)")
Edit after comments:
try changing this:
$location = New-Item -Path c:\test -ItemType Directory -Name ("XML_$(Get-Date -f dd_MM_yyyy_hhmmss)")
$outputFile= "$($location.fullname)\File_2.xml"
这篇关于如何使用Powershell中当前日期的名称创建文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!