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.<\/p>\n
09/24/2018 text files would go into 09/25/2018<\/p>\n
<I have something that looks like this so far, it works kinda><\/p>\n
clear<\/p>\n
$old = “C:\\temp\\2018\\09-24-20”<\/p>\n
if (Test-Path $old) \n{ \necho “File exists” \n} \nelse \n{ \nNew-Item -ItemType Directory -Path “C:\\temp\\2018\\09$((Get-Date).ToString(‘dd’))” -force<\/p>\n
}<\/p>\n
clear<\/p>\n
$new = “C:\\Temp\\2018\\09-24*.txt”<\/p>\n
if (Test-Path $new) \n{ \nGet-ChildItem -Path “C:\\temp\\2018\\09\\24*.txt” -Recurse | Move-Item -Destination \"C:\\temp\\2018\\09\\24\" \n}<\/p>","upvoteCount":2,"answerCount":8,"datePublished":"2018-09-25T10:18:50.000Z","author":{"@type":"Person","name":"anthonyginczycki","url":"https://community.spiceworks.com/u/anthonyginczycki"},"acceptedAnswer":{"@type":"Answer","text":"
Something like this then?<\/p>\n
$Yesterday = (Get-Date).AddDays(-1).ToString('dd')\n$Today = (Get-Date).ToString('dd')\nNew-Item -ItemType Directory C:\\temp\\2018\\09\\$Today\nMove-Item C:\\temp\\2018\\09\\$Yesterday\\* -Destination C:\\Temp\\2018\\09\\$Today\n<\/code><\/pre>\nSeems like an interesting way to do something like this but this should do what you’re looking for.<\/p>","upvoteCount":0,"datePublished":"2018-09-25T13:03:27.000Z","url":"https://community.spiceworks.com/t/powershell-date-and-time/675107/6","author":{"@type":"Person","name":"mikepolselli","url":"https://community.spiceworks.com/u/mikepolselli"}},"suggestedAnswer":[{"@type":"Answer","text":"
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.<\/p>\n
09/24/2018 text files would go into 09/25/2018<\/p>\n
<I have something that looks like this so far, it works kinda><\/p>\n
clear<\/p>\n
$old = “C:\\temp\\2018\\09-24-20”<\/p>\n
if (Test-Path $old) \n{ \necho “File exists” \n} \nelse \n{ \nNew-Item -ItemType Directory -Path “C:\\temp\\2018\\09$((Get-Date).ToString(‘dd’))” -force<\/p>\n
}<\/p>\n
clear<\/p>\n
$new = “C:\\Temp\\2018\\09-24*.txt”<\/p>\n
if (Test-Path $new) \n{ \nGet-ChildItem -Path “C:\\temp\\2018\\09\\24*.txt” -Recurse | Move-Item -Destination \"C:\\temp\\2018\\09\\24\" \n}<\/p>","upvoteCount":2,"datePublished":"2018-09-25T10:18:51.000Z","url":"https://community.spiceworks.com/t/powershell-date-and-time/675107/1","author":{"@type":"Person","name":"anthonyginczycki","url":"https://community.spiceworks.com/u/anthonyginczycki"}},{"@type":"Answer","text":"\n\n
<\/div>\n
G. Anthony:<\/div>\n
\nI’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.<\/p>\n<\/blockquote>\n<\/aside>\n
Try this<\/p>\n
$yesterday=(get-date).AddDays(-1).ToString('MM-dd-yyyy')\nMove-Item c:\\files -Destination c:\\folder\\$yesterday\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2018-09-25T10:43:19.000Z","url":"https://community.spiceworks.com/t/powershell-date-and-time/675107/2","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"Am i supposed to just use that code or add it to my existing?<\/p>","upvoteCount":0,"datePublished":"2018-09-25T11:30:33.000Z","url":"https://community.spiceworks.com/t/powershell-date-and-time/675107/3","author":{"@type":"Person","name":"anthonyginczycki","url":"https://community.spiceworks.com/u/anthonyginczycki"}},{"@type":"Answer","text":"\n\n
<\/div>\n
G. Anthony:<\/div>\n
\nAm i supposed to just use that code or add it to my existing?<\/p>\n<\/blockquote>\n<\/aside>\n
I suspect he’s given you a solution.<\/p>\n
The command:<\/p>\n
(Get-Date).AddDays(-1).ToString('MM-dd-yyyy')\n<\/code><\/pre>\nproduces a date string (“09-24-2018”). I am assuming that that’s the folder name you want to add.<\/p>","upvoteCount":0,"datePublished":"2018-09-25T12:22:51.000Z","url":"https://community.spiceworks.com/t/powershell-date-and-time/675107/4","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":"
No, I want to add text files from that folder.<\/p>\n
move-item “C:\\temp\\2018\\09\\24\\test.txt” -destination “C:\\temp\\2018\\09\\25”<\/p>\n
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.<\/p>","upvoteCount":0,"datePublished":"2018-09-25T12:40:59.000Z","url":"https://community.spiceworks.com/t/powershell-date-and-time/675107/5","author":{"@type":"Person","name":"anthonyginczycki","url":"https://community.spiceworks.com/u/anthonyginczycki"}},{"@type":"Answer","text":"
Yes, something like that. I’ll try it out and vote the best answer.<\/p>","upvoteCount":1,"datePublished":"2018-09-25T13:04:59.000Z","url":"https://community.spiceworks.com/t/powershell-date-and-time/675107/7","author":{"@type":"Person","name":"anthonyginczycki","url":"https://community.spiceworks.com/u/anthonyginczycki"}},{"@type":"Answer","text":"
Thanks titus. It works in my environment.<\/p>","upvoteCount":1,"datePublished":"2018-09-25T13:41:26.000Z","url":"https://community.spiceworks.com/t/powershell-date-and-time/675107/8","author":{"@type":"Person","name":"anthonyginczycki","url":"https://community.spiceworks.com/u/anthonyginczycki"}}]}}
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
jitensh
(JitenSh)
September 25, 2018, 10:43am
2
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?
DoctorDNS
(DoctorDNS)
September 25, 2018, 12:22pm
4
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