How to create a folder with a name based on the current date in Powershell?

I have about 50 xml files that are generated every time I run certain logic. Now I want these 50 files to be stored in a specific date folder. No matter how many times I run this logic for one specific date, xml files should only be overwritten for that specific date (based on hhmmss). Simple: How to create a folder with a name based on the current date and save xml files in it depending on the date?

For example: there are 3 xml files file_1.xml, file_2.xml and file_3.xml

Now I want to create a folder in the format

**xml_yyyymmdd_hhmmss** 

in which all xml files will be placed.

 For Eg: Xml_20121029_180912 

will be a folder created for today's date. And all 3 xml files will be saved in this for today.

Tomorrow the folder name will be:

 Xml_20121030_170912 

My code is as follows:

 $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 problem is here: the XML_29_10_2012_091717 folder is created with the 2.xml file in it, but not inside C: \ Test, but where is the script.

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

+4
source share
3 answers

Try the following:

 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" 
+4
source

Full version:

 New-Item -Path . -ItemType Directory -Name (Get-Date -f dd_MM_yyyy) 

You can also use md or mkdir

 md (Get-Date -f dd_MM_yyyy) 
+2
source
 $location = New-Item -Path $rptdir -ItemType Directory **-force** -Name ("XML_$(Get-Date -f dd_MM_yyyy_hhmmss)") 

How about adding the -force cmd-let command here?

0
source

Source: https://habr.com/ru/post/1442704/


All Articles