batch file to do sum bit thank you in advanced

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
teamsheetz
Posts: 1
Joined: 20 Aug 2010 11:04

batch file to do sum bit thank you in advanced

#1 Post by teamsheetz » 20 Aug 2010 11:06

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\

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#2 Post by !k » 20 Aug 2010 11:44

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"

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

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

#3 Post by orange_batch » 20 Aug 2010 18:33

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!

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#4 Post by !k » 21 Aug 2010 06:46

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"
)

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

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

#5 Post by orange_batch » 22 Aug 2010 01:01

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"

?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

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

#6 Post by !k » 23 Aug 2010 03:09

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

Post Reply