I’m trying to make a script that creates folders based on today’s date and moves text files from a folder from the day before.

09/24/2018 text files would go into 09/25/2018

<I have something that looks like this so far, it works kinda>

clear

$old = “C:\temp\2018\09-24-20”

if (Test-Path $old)
{
echo “File exists”
}
else
{
New-Item -ItemType Directory -Path “C:\temp\2018\09$((Get-Date).ToString(‘dd’))” -force

}

clear

$new = “C:\Temp\2018\09-24*.txt”

if (Test-Path $new)
{
Get-ChildItem -Path “C:\temp\2018\09\24*.txt” -Recurse | Move-Item -Destination "C:\temp\2018\09\24"
}

2 Spice ups

Try this

$yesterday=(get-date).AddDays(-1).ToString('MM-dd-yyyy')
Move-Item c:\files -Destination c:\folder\$yesterday
1 Spice up

Am i supposed to just use that code or add it to my existing?

I suspect he’s given you a solution.

The command:

(Get-Date).AddDays(-1).ToString('MM-dd-yyyy')

produces a date string (“09-24-2018”). I am assuming that that’s the folder name you want to add.

No, I want to add text files from that folder.

move-item “C:\temp\2018\09\24\test.txt” -destination “C:\temp\2018\09\25”

Then the batch would keep taking the file from the day before and move it to the current day. It’ll be a batch file set to run on a task sequence.

Something like this then?

$Yesterday = (Get-Date).AddDays(-1).ToString('dd')
$Today = (Get-Date).ToString('dd')
New-Item -ItemType Directory C:\temp\2018\09\$Today
Move-Item C:\temp\2018\09\$Yesterday\* -Destination C:\Temp\2018\09\$Today

Seems like an interesting way to do something like this but this should do what you’re looking for.

Yes, something like that. I’ll try it out and vote the best answer.

1 Spice up

Thanks titus. It works in my environment.

1 Spice up