Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
lgt
- Posts: 9
- Joined: 08 Dec 2014 17:09
#1
Post
by lgt » 27 Aug 2015 15:40
I have the following batch file that works. What I'm trying to do now is to delete files in the Documents folder that are not in the source folder. I can't seem to be able to do it. I tried robocopy with the mir option but it is not working. I'm hoping somebody will be able to help. Thanks.
Code: Select all
@echo
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
set logfile=C:\backup_log_file_"%mydate%_%mytime%".txt
set DevPotableWaterProjects=C:\Public Services New\DevPotable Water Projects
echo %Date% %Time% # # # Moving files >> %logfile%
for /r "%DevPotableWaterProjects%" %%a in ("*MTR*","*SWR*","*Plan Review*") do (
for %%b in ("%%~dpa\.") do (
echo processing "%%a" >> %logfile%
md "C:\Developer\%%~nxb\Documents" >> %logfile%
md "C:\Developer\%%~nxb\Attachments" >> %logfile%
xcopy "%%a" "C:\Developer\%%~nxb\Documents" /s /d /y
)
)
echo %Date% %Time% # # Moving Complete! >> %logfile%
Last edited by
lgt on 27 Aug 2015 21:44, edited 1 time in total.
-
ShadowThief
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
#2
Post
by ShadowThief » 27 Aug 2015 17:21
I haven't tried running this yet, but just at an initial glance, you should try changing the line
Code: Select all
set DevPotableWaterProjects=C:\Public Services New\DevPotable Water Projects
to
Code: Select all
set "DevPotableWaterProjects=C:\Public Services New\DevPotable Water Projects"
because the spaces in the target path name are breaking things.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#3
Post
by foxidrive » 27 Aug 2015 17:52
lgt wrote:I tried robocopy with the mir option but it is not working.
Is a mirror backup what you wanted in the first place?
Robocopy is the best tool for that... it only copies/removes what is necessary.
Try this on two test folders - it also nuls the retry and timeout features, which is suitable for a plain data backup on local drives.
Code: Select all
@echo off
set "datafolder=c:\files"
set "backupfolder=d:\archive\backup"
set switches=/r:0 /w:0 /mir
robocopy %switches% "%datafolder%" "%backupfolder%"
pause
-
lgt
- Posts: 9
- Joined: 08 Dec 2014 17:09
#4
Post
by lgt » 27 Aug 2015 21:55
Your code worked when run as is but the problem I'm having is incorporating robocopy into my batch file, it's not working for me. I need a way to delete files that are no longer in the source folder while keeping the logic of my current script. I managed a workaround by first deleting the folder and then copying the files again but I was hoping for a cleaner way to do it. Thank you!
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#5
Post
by foxidrive » 27 Aug 2015 23:44
lgt wrote:Your code worked when run as is but the problem I'm having is incorporating robocopy into my batch file, it's not working for me.
Can you explain which part you are having trouble with, the error messages, what is failing, etc?
There may be easy modifications to make it suit your code.
-
lgt
- Posts: 9
- Joined: 08 Dec 2014 17:09
#6
Post
by lgt » 31 Aug 2015 15:29
This is my folder structure:
Project Folder
-Documents
-Attachments
I have many project folders in the same directory. I need to be able to go through each folder and delete anything in the "Documents" folder that is not in the source folder. I only need to do this for the "Documents" folder and not for the "Attachments" folder. I'm having trouble using robocopy to loop through each folder and do this.
I hope this makes sense.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#7
Post
by foxidrive » 31 Aug 2015 17:14
lgt wrote:This is my folder structure:
Project Folder
-Documents
-Attachments
I have many project folders in the same directory. I need to be able to go through each folder and delete anything in the "Documents" folder that is not in the source folder. I only need to do this for the "Documents" folder and not for the "Attachments" folder. I'm having trouble using robocopy to loop through each folder and do this.
I hope this makes sense.
You can exclude the attachments folder using this in robocopy /xd "attachments"
-
lgt
- Posts: 9
- Joined: 08 Dec 2014 17:09
#8
Post
by lgt » 01 Sep 2015 10:57
I'm trying to run this but I'm getting Error 267, "The directory name is invalid".
Code: Select all
set "DevPotableWaterProjects=\\owd-fp1\facilitybook\Projects\Leonel\infraMAP\Inspection\Public Services New\DevPotable Water Projects"
for /r "%DevPotableWaterProjects%" %%a in ("*MTR*","*SWR*","*Plan Review*") do (
for %%b in ("%%~dpa\.") do (
echo processing "%%a
robocopy /mir "%%a" "\\owd-fp1\facilitybook\Projects\Leonel\infraMAP\Inspection\Temp\iWater Inspection Attachments\Developer\%%~nxb\Documents" >> %logfile%
)
)
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#9
Post
by foxidrive » 01 Sep 2015 18:33
Place
echo on the same line before the robocopy command and you will see what is being used in the command line.
Didn't you like the idea of excluding the attachments folder?
-
lgt
- Posts: 9
- Joined: 08 Dec 2014 17:09
#10
Post
by lgt » 02 Sep 2015 10:38
It's not that I didn't like your idea of excluding the attachments folder it's that I'm not sure how to incorporate it into my current batch file.