Help with code

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tiagocampos
Posts: 38
Joined: 21 Feb 2013 12:41

Help with code

#1 Post by tiagocampos » 21 Mar 2013 10:51

Code: Select all

@echo off

set sourceFolder=%~dp0
SET destinationFolder=%sourceFolder:~0,-9%

echo %sourceFolder% 
echo %destinationFolder%

robocopy  %sourceFolder% %destinationFolder% /xf desarquivador.bat /mov


Hello all,

I've made this code to move all files from some directories to it's parent directory.
All directories are similar, example:

E:\ftp\SLSBRTLM\Archive
E:\ftp\SLSTSOFI\Archive
E:\ftp\SLCNASLC\Archive
E:\ftp\SLSPGLRE\Archive
E:\ftp\SLSTRLC1\Archive

What I need is this to be done from a .txt file where I have all the directories where I want this script to run. I tried to do it but seems that I made some errors, can you assist me?

Code: Select all

@echo off
set file=C:\file.txt

for /f "delims=" %%a in (%file%) do (

set sourceFolder=%%a
set destinationFolder=%sourceFolder:~0,-9%

echo %sourceFolder% 
echo %destinationFolder%

robocopy  %sourceFolder% %destinationFolder% /mov
)

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: Help with code

#2 Post by mfm4aa » 21 Mar 2013 11:15

My suggestion:

Code: Select all

@echo off &setlocal
set "file=C:\file.txt"

for /f "delims=" %%i in (%file%) do call :process "%%~i"
goto :eof

:process
set "sourceFolder=%~1"
SET "destinationFolder=%sourceFolder:~0,-9%"

echo %sourceFolder%
echo %destinationFolder%

robocopy  "%sourceFolder%" "%destinationFolder%" /xf desarquivador.bat /mov
goto :eof

endlocal

tiagocampos
Posts: 38
Joined: 21 Feb 2013 12:41

Re: Help with code

#3 Post by tiagocampos » 21 Mar 2013 12:11

Perfect, thank you very much :)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Help with code

#4 Post by Squashman » 21 Mar 2013 12:37

Or you could have used delayed expansion.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: Help with code

#5 Post by mfm4aa » 21 Mar 2013 12:48

Yes. But the for loop deletes exclamation marks with delayed expansion. Don't know, if there are some of them .. :)

Post Reply