luketr
asked on
Powershell script to create folder with time stamp.
Hi,
I need to write a powershell script that will create a folder with today’s time stamp and move all the files in the directory to the folder that has just been created with the time stamp.
Thanks
Luke
I need to write a powershell script that will create a folder with today’s time stamp and move all the files in the directory to the folder that has just been created with the time stamp.
Thanks
Luke
Line 2 above is for debugging only, and can be removed. Besides that, the code is not working that way, the foreach-object (%) is wrong here, but should work if %{ and the closing } are removed.
For my taste there is too much of get-date above - that might not perform well with a lot of files.
For my taste there is too much of get-date above - that might not perform well with a lot of files.
$src = C:\Temp
$target = C:\Temp\(get-date).ToString("yyyyMMdd")
$yesterday = (get-date).adddays(-1).ToString("yyyyMMdd")
gci $src | ? {$_.lastwritetime.ToString("yyyyMMdd") -eq $yesterday} | move-item $target
ASKER
When i try and run the script i get the following error.
PS C:\Documents and Settings\dipadmin> $src = C:\Temp
The term 'C:\Temp' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try a
gain.
At line:1 char:14
+ $src = C:\Temp <<<<
PS C:\Documents and Settings\dipadmin> $target = C:\Temp\(get-date).ToStrin g("yyyyMMd d")
The term 'C:\Temp\' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try
again.
At line:1 char:19
+ $target = C:\Temp\( <<<< get-date).ToString("yyyyMM dd")
PS C:\Documents and Settings\dipadmin> $yesterday = (get-date).adddays(-1).ToS tring("yyy yMMdd")
PS C:\Documents and Settings\dipadmin> gci $src | ? {$_.lastwritetime.ToString ("yyyyMMdd ") -eq $yesterday} | move-item $
target
PS C:\Documents and Settings\dipadmin> $src = C:\Temp
The term 'C:\Temp' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try a
gain.
At line:1 char:14
+ $src = C:\Temp <<<<
PS C:\Documents and Settings\dipadmin> $target = C:\Temp\(get-date).ToStrin
The term 'C:\Temp\' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try
again.
At line:1 char:19
+ $target = C:\Temp\( <<<< get-date).ToString("yyyyMM
PS C:\Documents and Settings\dipadmin> $yesterday = (get-date).adddays(-1).ToS
PS C:\Documents and Settings\dipadmin> gci $src | ? {$_.lastwritetime.ToString
target
Sorry, the first line needs to sound:
$src = "C:\Temp"
$src = "C:\Temp"
ASKER
it's erroring on the 2nd line
$target = "C:\Temp\"(get-date).ToStr ing("yyyyM Mdd")
The term 'C:\Temp\' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try
again.
At line:1 char:19
+ $target = C:\Temp\( <<<< get-date).ToString("yyyyMM dd")
$target = "C:\Temp\"(get-date).ToStr
The term 'C:\Temp\' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try
again.
At line:1 char:19
+ $target = C:\Temp\( <<<< get-date).ToString("yyyyMM
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
New-Item "C:\temp\$(get-date -f yyyyMMdd)" -type directory
To move the file
Open in new window