Computer Hope

Microsoft => Microsoft DOS => Topic started by: Law506 on July 25, 2008, 11:06:53 PM

Title: Folder Transfer Batch File
Post by: Law506 on July 25, 2008, 11:06:53 PM
Hi,

I am stuck on how to write a batch file to move a group of folders.

What I am doing is that I have a folder full of folders that in turn are full of PDF files.  These are labeled with peoples' names and are listed in Alphabetical Order based on Windows Folder sorting.  I want to move these folders and thier files to another directory on the hard drive. 

This is the catch, the desintation directory has 26 folders in it that are labeled "A", "B", "C", etc...  I want the batch file to be able to use a wildcard or query or something on the source folder with all of the named folders in it, I want it to be able to read the first letter of the folder name, example "A" and then move all of those folders (patients' names) to the desitnation directoy folder "A".  Then do another command to search for folders begining with "B" in the source folder and then have them send to the desination folder sub directory "B". 

Hopefully this is clear, any clarification needed please ask.  I tried using for commands and such.. not such luck.

Thanks.
Title: Re: Folder Transfer Batch File
Post by: Dias de verano on July 25, 2008, 11:43:45 PM
Looks like you may have the same homework assignment as Gitman7

http://www.computerhope.com/forum/index.php/topic,62209.0.html

Title: Re: Folder Transfer Batch File
Post by: erobby on July 26, 2008, 01:04:05 AM
just a quick thought but try something like this

set dpath=Destination of folder containing alphabetical folder
for %%a in ('dir /s/b *.pdf') do (
     set fname=%~nI
     if a== %fname:~0,1% copy %%a %dpath%\a
     if b== %fname:~0,1% copy %%a %dpath%\b
     etc until you reach "Z"
)

This may work

Title: Re: Folder Transfer Batch File
Post by: Dias de verano on July 26, 2008, 01:15:22 AM
just a quick thought but try something like this

set dpath=Destination of folder containing alphabetical folder
for %%a in ('dir /s/b *.pdf') do (
     set fname=%~nI
     if a== %fname:~0,1% copy %%a %dpath%\a
     if b== %fname:~0,1% copy %%a %dpath%\b
     etc until you reach "Z"
)

This may work



It certainly won't! Anyway, he wants to move, not copy. Erobby, welcome to Chope and all that stuff. I don't want to dampen your enthusiasm, and God knows a guy's gotta get his post count up somehow, but could you perhaps stick to posting about things you actually know something about?
Title: Re: Folder Transfer Batch File
Post by: erobby on July 26, 2008, 01:23:29 AM
DIAS,

Thanks for the warm greeting I guess replacing move with copy would be a tragedy and we wouldn't want that would we?

Maybe you should read my post until then I suggest stick to comments about people that you know.

But once again thanks for your input.  It's always a pleasure to hear from someone with inflated opinion of themselves.
Title: Re: Folder Transfer Batch File
Post by: Law506 on July 26, 2008, 03:48:39 PM
Looking at the code it looks like stuff I have seen, but .pdf is what I am trying to get away from.   I dont want the batch to search the source folders for pdf files, I want it to search the master source folder and move the folders that A**** then move on to B***, etc.  I cant find jack on that code.  I appreciate the code, but still not quite what I am after in terms of moving the folder.

Dias, thanks for the link, I am looking at it now... but it looks like he is moving files into sorted folders which is half of what I need.  How can you modify the first part to search the master folder for folders (not the files) begining with A** and etc and have the batch toss those folders into the destination folder "A"?

Thanks.
Title: Re: Folder Transfer Batch File
Post by: GuruGary on July 26, 2008, 09:18:21 PM
You could do something like this:

Code: [Select]
@echo off
setlocal enabledelayedexpansion
set Source=D:\Path\to\source
set Target=D:\Path\to\destination

for /f "tokens=*" %%a in ('dir "%Source%" /ad /b') do (
   set Current=%%a
   move "%Source%\%%a" "%Target%\!Current:~0,1!"
   if errorlevel 1 echo Couldn't move %%a
   )
Title: Re: Folder Transfer Batch File
Post by: Law506 on July 26, 2008, 11:18:57 PM
Tried that code and it has proven to be a huge step in the right direction.

However, when I run it, it copies all folders and places them into the correct destination folder, but it does all folders but one.  The first folder it encounters it sends over just the files of it and then sends over the rest of the folders behind it as it should be.  Not sure why.

Also, how can it be made to overwrite the folder if it already exists.  Some people come back and have the same folder name but the PDF info changes and when you copy manually it says do you want to overwrite the folder (Which will merge the files together) and this is good.  Is there a command for this in a batch file?

Thanks for the help guys, I appreciate it.
Title: Re: Folder Transfer Batch File
Post by: Dias de verano on July 27, 2008, 02:02:58 AM
Hi,

I am stuck on how to write a batch file to move a group of folders.

What I am doing is that I have a folder full of folders that in turn are full of PDF files.  These are labeled with peoples' names and are listed in Alphabetical Order based on Windows Folder sorting.  I want to move these folders and thier files to another directory on the hard drive. 

This is the catch, the desintation directory has 26 folders in it that are labeled "A", "B", "C", etc...  I want the batch file to be able to use a wildcard or query or something on the source folder with all of the named folders in it, I want it to be able to read the first letter of the folder name, example "A" and then move all of those folders (patients' names) to the desitnation directoy folder "A".  Then do another command to search for folders begining with "B" in the source folder and then have them send to the desination folder sub directory "B". 


So we have this type of situation? <folder name> shown thus


<source folder>
     <Adams>
     <Brown>
     <Collins>
     <Davis>
     <Edwards>
     <... etc ...>

<Destination folder>
     <A>
         \
          <Adams>
     <B>
         \
          <Brown>     
     <C>
         \
          <Collins>
     <D>
         \
          <Davis>
     <E>
         \
          <Edwards>
     <... etc ... >


Have I got that right? So if source folder contains folders Adams, Aaronson, Attwater, they would all be moved (you do mean moved and not copied?) so they are under the folder <A>  under the destination folder? And so on? And if the folder already exists, files with the same name will overwrite the ones already there?

This sure sounds like a job for XCOPY.


Title: Re: Folder Transfer Batch File
Post by: Law506 on July 27, 2008, 12:55:33 PM
That is pretty much the idea.  I am new to writing these things, I have looked at xcopy as well, but not sure what do with it either.  I do want to move these files and overwrite the destination one if it exists, so it will combine the older files within with the new ones.  BUT, If I can get Xcopy to work, I can go in a manually delete the original folders myself, I dont mind that. :)
Title: Re: Folder Transfer Batch File
Post by: GuruGary on July 27, 2008, 02:21:00 PM
Using XCOPY is the way to go of you want to combine the contents.  I'm not sure why the first directory gave you problems the last time, but you can use the same variables and try this:
Code: [Select]
for /f "tokens=*" %%a in ('dir "%Source%" /ad /b') do (
   set Current=%%a
   xcopy "%Source%\%%a" "%Target%\!Current:~0,1!\%%a\" /S /D /Y /C
   )

If you want to copy hidden and system files, then you should also add /H /K to the XCOPY switches.

You could also automate the deletion of the source directories, but I would be very careful about doing that without adding some significant error checking to make sure the copy worked properly first.
Title: Re: Folder Transfer Batch File
Post by: Dias de verano on July 27, 2008, 02:27:58 PM
You could also automate the deletion of the source directories, but I would be very careful about doing that without adding some significant error checking to make sure the copy worked properly first.

That is very, very good advice.

Also, you can add the /L (list) switch to xcopy to make it list the files that it would copy, without actually copying them.


Title: Re: Folder Transfer Batch File
Post by: Law506 on July 27, 2008, 03:02:37 PM
Awesome, I had changed the original command that GuruGary provided to include XCOPY but I left some stuff out now that I can see.  It worked like a charm on my first trial test run.

Thanks Guys, I'll use this bad boy at work this week after I test it a little more to make very sure.  Can't afford to have a file loss :).

Thanks Again.
Title: Re: Folder Transfer Batch File
Post by: erobby on July 27, 2008, 03:43:06 PM

This works as well

Code: [Select]
setlocal enabledelayedexpansion
set dpath=Destination of folder containing alphabetical folder
for /f "delims=;" %%a in ('dir /s/b *.pdf') do (
     set fname=%%~na%
     call :mover %%a
)

:mover
if a==%fname:~0,1% move "%*" %dpath%\a
if b==%fname:~0,1% move "%*" %dpath%\b
if c==%fname:~0,1% move "%*" %dpath%\c
if d==%fname:~0,1% move "%*" %dpath%\d
until you reach "Z"
goto :eof

Here is the output

C:\>(
set fname=roguecheat
 call :mover C:\Games\rogue\rogueomac\roguecheat.pdf
)

C:\>if a == r move "C:\Games\rogue\rogueomac\roguecheat.pdf" C:\hold\a
C:\>if b == r move "C:\Games\rogue\rogueomac\roguecheat.pdf" C:\hold\b
C:\>if c == r move "C:\Games\rogue\rogueomac\roguecheat.pdf" C:\hold\c
C:\>if d == r move "C:\Games\rogue\rogueomac\roguecheat.pdf" C:\hold\d
C:\>goto :eof


C:\>(
set fname=ac3filter_eng
 call :mover C:\Program Files\AC3Filter\doc\ac3filter_eng.pdf
)

C:\>if a == a move "C:\Program Files\AC3Filter\doc\ac3filter_eng.pdf" C:\hold\a
C:\>if b == a move "C:\Program Files\AC3Filter\doc\ac3filter_eng.pdf" C:\hold\b
C:\>if c == a move "C:\Program Files\AC3Filter\doc\ac3filter_eng.pdf" C:\hold\c
C:\>if d == a move "C:\Program Files\AC3Filter\doc\ac3filter_eng.pdf" C:\hold\d
C:\>goto :eof


C:\>(
set fname=ac3filter_rus
 call :mover C:\Program Files\AC3Filter\doc\ac3filter_rus.pdf
)

C:\>if a == a move "C:\Program Files\AC3Filter\doc\ac3filter_rus.pdf" C:\hold\a
C:\>if b == a move "C:\Program Files\AC3Filter\doc\ac3filter_rus.pdf" C:\hold\b
C:\>if c == a move "C:\Program Files\AC3Filter\doc\ac3filter_rus.pdf" C:\hold\c
C:\>if d == a move "C:\Program Files\AC3Filter\doc\ac3filter_rus.pdf" C:\hold\d
C:\>goto :eof
Title: Re: Folder Transfer Batch File
Post by: Law506 on August 01, 2008, 09:43:09 PM
hey,
program appears to be working like a charm, can't thank yall enough.  I have modifed it a bit to output to a text file for a kind of record keeping thing.  What command would I throw into the batch file to have it output in the text file how many FOLDERS it moved?  or is this possible?  I would be happy I guess with the number of files moved if that one is possible. 

Thanks guys, appreciate it.

If anyone knows off the top of thier head, I am looking around for the info now, how to setup an error log for a file not transfered to throw into the batch file that would be awesome.

Here is the file that has been compiled and modified slightly.

Code: [Select]
@echo off
cls
echo -------------------------------------------------------------------------------------------------------- >>Transfer.txt
date /t >>Transfer.txt
time /t >>Transfer.txt
echo This will transfer all folders in SCAN to the ALPHABETICAL SCANNED DOCUMENTS.
echo To stop the process CLOSE this window.
echo List of Files Moved. >>Transfer.txt
pause

setlocal enabledelayedexpansion
set Source=C:\Scanned Documents\
set Target=C:\Alphabetical Scanned Documents\

for /f "tokens=*" %%a in ('dir "%Source%" /ad /b') do (
   set Current=%%a
   xcopy "%Source%\%%a" "%Target%\!Current:~0,1!\%%a\" /S /D /Y /C >>Transfer.txt
   )

echo End Of Copy, see Transfer.txt for list of moved files.
echo End Of Copy. >>Transfer.txt
pause
Title: Re: Folder Transfer Batch File
Post by: Dias de verano on August 01, 2008, 11:33:55 PM
What command would I throw into the batch file to have it output in the text file how many FOLDERS it moved?

@echo off
cls
echo -------------------------------------------------------------------------------------------------------- >>Transfer.txt
date /t >>Transfer.txt
time /t >>Transfer.txt
echo This will transfer all folders in SCAN to the ALPHABETICAL SCANNED DOCUMENTS.
echo To stop the process CLOSE this window.
echo List of Files Moved. >>Transfer.txt
pause

setlocal enabledelayedexpansion
set Source=C:\Scanned Documents\
set Target=C:\Alphabetical Scanned Documents\

set /a foldersmoved=0

for /f "tokens=*" %%a in ('dir "%Source%" /ad /b') do (
   set Current=%%a
   xcopy "%Source%\%%a" "%Target%\!Current:~0,1!\%%a\" /S /D /Y /C >>Transfer.txt
   set /a foldersmoved=!foldersmoved!+1
   )

echo End Of Copy, see Transfer.txt for list of moved files.
echo End Of Copy. >>Transfer.txt
echo Folders moved: %foldersmoved% >> Transfer.txt
pause
Title: Re: Folder Transfer Batch File
Post by: Law506 on August 02, 2008, 11:25:58 PM
Awesome, works like a charm just like I expected.

Thanks Again.
Title: Re: Folder Transfer Batch File
Post by: erobby on August 03, 2008, 08:51:25 AM
hey,
program appears to be working like a charm, can't thank yall enough.  I have modifed it a bit to output to a text file for a kind of record keeping thing.  What command would I throw into the batch file to have it output in the text file how many FOLDERS it moved?  or is this possible?  I would be happy I guess with the number of files moved if that one is possible. 

Thanks guys, appreciate it.

If anyone knows off the top of thier head, I am looking around for the info now, how to setup an error log for a file not transfered to throw into the batch file that would be awesome.

Here is the file that has been compiled and modified slightly.

Code: [Select]
@echo off
cls
echo -------------------------------------------------------------------------------------------------------- >>Transfer.txt
date /t >>Transfer.txt
time /t >>Transfer.txt
echo This will transfer all folders in SCAN to the ALPHABETICAL SCANNED DOCUMENTS.
echo To stop the process CLOSE this window.
echo List of Files Moved. >>Transfer.txt
pause

setlocal enabledelayedexpansion
set Source=C:\Scanned Documents\
set Target=C:\Alphabetical Scanned Documents\

for /f "tokens=*" %%a in ('dir "%Source%" /ad /b') do (
   set Current=%%a
   xcopy "%Source%\%%a" "%Target%\!Current:~0,1!\%%a\" /S /D /Y /C >>Transfer.txt
   )

echo End Of Copy, see Transfer.txt for list of moved files.
echo End Of Copy. >>Transfer.txt
pause

for /f "tokens=*" %%a in ('dir "%Source%" /ad /b') do (
   set Current=%%a
   xcopy "%Source%\%%a" "%Target%\!Current:~0,1!\%%a\" /S /D /Y /C >>Transfer.txt
   If %Errorlevel%==0 (
                GOTO :EOF
    ) else (
                Echo The following file completed with %errorlevel% >> errorlog.txt
    )
)
Title: Re: Folder Transfer Batch File
Post by: Dias de verano on August 03, 2008, 09:00:02 AM
what "following file"? Can't quite see how the filename gets echoed.
Title: Re: Folder Transfer Batch File
Post by: erobby on August 03, 2008, 09:04:07 AM
what "following file"? Can't quite see how the filename gets echoed.


Sorry should have read "following %%a"
Title: Re: Folder Transfer Batch File
Post by: Dias de verano on August 03, 2008, 10:49:18 AM
but the dir /ad in the loop makes %%a a folder, not a file.
Title: Re: Folder Transfer Batch File
Post by: erobby on August 03, 2008, 04:46:49 PM
but the dir /ad in the loop makes %%a a folder, not a file.

Yes you are correct again.  My bad for not reading the script carefully, I guess I was focused on the script that I posted and remember that the search was for .pdf's.

No I didn't make the assumption that the only files in any folder would be PDF's.  But thanks again for pointing out my short sightedness DIAS.