2
0
My goal is to have a .bat that I can use to automatically archive folders via copy, rename the original folder and the files within with the current date and then clean out old production files.
- Copy target folders to the "Completed" directory
- Update date on the source folders using the format
Previous Name_Today's Date
(MMDDYY) - Update the date of the INDD files within the source folder using the format
Current Name_Today's Date
(MMDDYY) - "Clean" the source folder by deleting all .VPS and .PDFs files within the source folder
I am a novice but I have pieced together this code from research and sources:
@echo off
setlocal enabledelayedexpansion
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%
xcopy /s /e /q /y "G:\...\Annual_*" "G:\...\_DONE\"
xcopy /s /e /q /y "G:\...\Life_*" "G:\...\_DONE\"
MOVE "G:\...\Annual_*" "G:\...\Annual_today"
MOVE "G:\...\Life_today" "G:\...\Life_today"
FOR /M *.indd /C "cmd /c rename @file \"@fname - today.indd\""
del /s "G:\...\Annual_today" *.pdf
del /s "G:\...\Annual_today" *.vps
del /s "G:\...\Life_today" *.pdf
del /s "G:\...\Life_today" *.vps
"G:\...\New_Job.bat" > output.txt
My end goal is to be able to change the directory path for the source and archive folders so I can reuse this script for different clients files.
Current Issues
As it stands the script doesn't copy and create an archive folder files and just, deletes all VPS and PDF files from all directories, not just the target folder.
I'm not sure if I'm performing the date check correctly to then use it as a variable to rename future folders and files.
I don't know if FOR /M *.indd /C "cmd /c rename @file \"@fname - today.indd\""
is correct to rename the files. The names are a PO number (6digits) then a title, underscore and then the date.
123456_Life_Kit_020819 for example.
Help would be greatly appreciated!
Here's an Example of what the script should do
Copies the WHOLE folder to the _OLD/Archive folder for each one. Then renames the folder and the contents extensions to the "current date". Then deletes the .pdf and .vps files in the NEW date directories only.
Here is an example of the folder structure.
Main directory:
Inside one of the subdirectories:
The only things I'm trying to rename are the MAIN directory folders with the dates (after copying) and then the files within the subdirectory.
No other folders need to be renamed.
1It's a little hard for me to clearly read what directories exist and where do you want to move your files. can you create an example folder structure how it looks like before the script runs and a folder structure how it should look like after it ran? And also if we should backup all files or just certain extensions like .indd. Also, would PowerShell be ok instead of batch? Maybe someone is able to help you in batch, but I can only do PowerShell. I think this is a bit easier to achieve in PowerShell really. – SimonS – 2019-02-11T15:18:29.590
@SimonS Sure thing! I updated my post with a video and more description. – Ovaryraptor – 2019-02-11T18:34:14.987
two last questiosn - what should we do with the folders and files that do not have a _mmddyy at the end of their name? leave them like they are or just add _mmddyy ? also, is the _OLD folder always in the same folder as the work folder you want to back up? – SimonS – 2019-02-12T11:45:08.563
1An example of what this script is supposed to do, would be great it it was textual rather than an image. – harrymc – 2019-02-12T12:01:20.523
@SimonS Powershell is fine I was just working in .bat because of my limited knowledge. I updated my post with images of the folder structures I'm working with. – Ovaryraptor – 2019-02-12T17:43:37.233
@Ovaryraptor Did you test the PowerShell scripts provided on either of the answers yet. If so, I was curious if either of those helped solve the problem or if you still need further help with some script logic for the job? – Pimp Juice IT – 2019-02-14T14:26:59.770
robocopy can do amazing things with one line of code. https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
– HackSlash – 2019-02-14T20:35:19.993@PimpJuiceIT So far neither of the posted solutions are working for me. – Ovaryraptor – 2019-02-14T21:08:16.507
@HackSlash I looked at robocopy initially but the issue lies with renaming the internal files and clearing out the unwanted file types. – Ovaryraptor – 2019-02-14T21:08:54.113
@PimpJuiceIT It's not just a 1-day increase, it varies. It could be, 021419 or 021919 or 022219 for the next job etc. The archive would be the previous job+all files with MMDDYY. The dates on the files matter because I potentially have to audit files if something goes awry and I need to cross-reference the current run with past runs. Having the _MMDDYY on the .indd files is very necessary – Ovaryraptor – 2019-02-14T21:22:48.130
1@PimpJuiceIT The archive directory (DONE) never needs to be touched. The starting folder layout is a _DONE directory, Annual_021419, Life_021419. Those 2 folders need to be copied into the _DONE and then the folder _and the files within need to be updated from the *_021419 to the current date (pretend I'm running this on 022219). Does that help clear things up? – Ovaryraptor – 2019-02-14T22:01:47.697
@Ovaryraptor ... There you go, check out my posted answer (and comment below it) when you get a chance. Consider it a special delivery just for you from me. I hope you can put it to use, but the PowerShell was not too bad; I tried to keep it as simple as possible logic wise. In any event, you can run it like a pure batch script to do what's needed as per my interpretation, understand, and extensive testing. – Pimp Juice IT – 2019-02-15T03:43:21.907
@PimpJuiceIT yah, that's probably the problem with my answer. too flexible logic wise, that's why it's hard to understand... Even though i tested every scenario, and it works like a charm. I hope my comments will make it a bit clearer – SimonS – 2019-02-15T08:49:35.050
I think our scripting may be okay but we have a new question about executing scripts on a server/nas drive and getting the folder targeting correct. – Brian – 2019-02-15T19:27:06.737
I think both answers are great and already +1 both of them before I added my answer by the way since I liked them so much. Here's both screen shots to prove it too by the way https://i.imgur.com/2jcBuNf.png and https://i.imgur.com/NrBvpfp.png as you will see both have a blue upvote from me since I'm the one that voted them they are blue. I only added my solution after hearing the OP was having trouble hoping the PowerShell batch hybrid solution would help solve the problem. The issue may also be with the version of PowerShell too but still have some troubleshooting to do. Thanks guys!!
– Pimp Juice IT – 2019-02-15T19:56:43.610