如何使用Powershell中当前日期的名称创建文件夹? [英] How to create a folder using a name based on the current date in Powershell?

查看:1173
本文介绍了如何使用Powershell中当前日期的名称创建文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约50个xml文件,每当我运行一个特定的逻辑是新生成的。现在我希望将这50个文件存储在特定的日期文件夹中。无论我在一个特定的日期里运行了多少次,xml文件只能在特定的日期被覆盖(基于hhmmss)。简单的说,如何使用基于当前日期的名称创建一个文件夹,并根据日期存储xml文件?



例如:有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆