Page 1 of 1

batch file to do sum bit thank you in advanced

Posted: 20 Aug 2010 11:06
by teamsheetz
i have a notepad file with a list of folder names. what i need is a batch file that will read from the text file and copy the folders and move it to a different directory then delete the folder afterwords. the folders are
E:\Student Home Directories\ESHomeDir
and needs to move to
E:\Student Home Directories\HS
the text file will be located in
E:\Student Home Directories\ES_Student_Dir _2B_deleted.txt

Thanks in Advance
and i plan on placing the batch file in the same directory
E:\Student Home Directories\

Re: batch file to do sum bit thank you in advanced

Posted: 20 Aug 2010 11:44
by !k

Code: Select all

@echo off
setlocal enableextensions

set "src=ESHomeDir"
set "trgt=HS"
set "lst=ES_Student_Dir_2B_deleted.txt"

for /f "delims=" %%d in (%lst%) do (
xcopy "%src%\%%d\*" "%trgt%\%%d" /i /s/e /k/r/h /q && rd /s /q "%src%\%%d"
)
Format of ES_Student_Dir_2B_deleted.txt is:
1st Student
2nd Student
3rd Student

Or if format is:
1st Student\
2nd Student\
3rd Student\
then remove "\" symbol from code "%src%\%%d\*"

Code: Select all

xcopy "%src%\%%d*" "%trgt%\%%d" /i /s/e /k/r/h /q && rd /s /q "%src%\%%d"

Re: batch file to do sum bit thank you in advanced

Posted: 20 Aug 2010 18:33
by orange_batch
You've almost got it right. I haven't used xcopy lately so I won't pretend to know what all the switches do, however, following your example this should do the job. Use enabledelayedexpansion to check if the current line ends with a \ or not. If it doesn't, append a \.

Code: Select all

@echo off
setlocal enableextensions
setlocal enabledelayedexpansion

set "src=ESHomeDir"
set "trgt=HS"
set "lst=ES_Student_Dir_2B_deleted.txt"

for /f "delims=" %%d in (%lst%) do (
set "step=%%d"
if "!step:~-1!" NEQ "\" set "step=%%d\"
xcopy "%src%\!step!*" "%trgt%\!step!" /i /s/e /k/r/h /q && rd /s /q "%src%\!step!"
)


If you need to remove the backslash anywhere, switch !step! with !step:~0,-1!

Re: batch file to do sum bit thank you in advanced

Posted: 21 Aug 2010 06:46
by !k
orange_batch wrote:Use enabledelayedexpansion...
This does not work if in the name presence exclamation mark ;)
The next code works with all these entries:
1st Student
2nd Student\
Stupidest Student!

Code: Select all

@echo off
setlocal enableextensions

set "src=ESHomeDir"
set "trgt=HS"
set "lst=ES_Student_Dir_2B_deleted.txt"

for /f "delims=" %%d in (%lst%) do (
xcopy "%src%\%%d.\*" "%trgt%\%%d" /i /s/e /k/r/h /q && rd /s /q "%src%\%%d"
)

Re: batch file to do sum bit thank you in advanced

Posted: 22 Aug 2010 01:01
by orange_batch
For some reason I confused you with the OP. :roll: I must have been sleepy.

!k could you explain how the inclusion of . changes the behaviour of:
xcopy "%src%\%%d.\*" "%trgt%\%%d" /i /s/e /k/r/h /q && rd /s /q "%src%\%%d"

?

Re: batch file to do sum bit thank you in advanced

Posted: 23 Aug 2010 03:09
by !k
orange_batch wrote:could you explain how the inclusion of . changes the behaviour
The point neutralizes trailing backslash.

Code: Select all

@echo off
for %%i in ("%~dp0") do echo StartDirName = %%~nxi
for %%i in ("%~dp0.") do echo StartDirName = %%~nxi
pause
Why? I do not know, because I do not coded cmd.exe