Page 1 of 1

move Files

Posted: 26 Apr 2008 17:32
by prashob12
I want a batch file to move files from different folders present in the same path.

For E.g. The folders containing xml files are as follows:

d:\output\XML\vgh_he_2006-03-14_12-a-2659-04
d:\output\XML\vgh_he_2006-03-14_11-ue-1426-04
d:\output\XML\vgh_he_2006-03-10_5-g-354-06
d:\output\XML\vg_weimar_2006-02-21_4-k-5500-04-we

etc.

Here there is a second condition where each folder has total of five different xml files which we can identify by their filename.

For E.g.

d:\output\XML\vgh_he_2006-03-14_12-a-2659-04 contains

1) vgh_he_2006-03-14_12-a-2659-04_vt.xml
2) vgh_he_2006-03-14_12-a-2659-04_jurion.xml
3) vgh_he_2006-03-14_12-a-2659-04_ls.xml
4) vgh_he_2006-03-14_12-a-2659-04_pm.xml
5) vgh_he_2006-03-14_12-a-2659-04_cas.xml

What i need is to create five types of folders namely

vt
cas
ls
pm
jurion

in d:\output\XML

move the xml files present in the folder

d:\output\XML\vgh_he_2006-03-14_12-a-2659-04 depending on their file name.

i.e.

vgh_he_2006-03-14_12-a-2659-04_vt.xml will move to vt
vgh_he_2006-03-14_12-a-2659-04_cas.xml will move to cas
vgh_he_2006-03-14_12-a-2659-04_jurion.xml will move to jurion
vgh_he_2006-03-14_12-a-2659-04_pm.xml will move to pm
vgh_he_2006-03-14_12-a-2659-04_ls.xml will move to ls

The same need to be repeated to all the folders present in d:\output\XML


I already asked about this in the past also but no Reply

This time hoping to get a reply

If what I explained is not able to understand let me know.

Regards
Prashob

Posted: 29 Apr 2008 00:13
by DosItHelp
prashob12,

This looks simpler than your previews post.

Code: Select all

@echo off
for /f "delims=" %%F in ('dir /b /s "XML\*.xml"') do (
    for /f "tokens=4 delims=_" %%D in ("%%~nF") do (
        echo.md XML2\%%D 2>NUL
        echo.move /Y "%%F" "XML2\%%D"
    )
)
pause

The first/outer FOR loop iterates all xml files in all sub folders using variable %%F.
%%~nF gives you the name of the xml file without extension.
The second/inner FOR loop extracts the folder name from the filename which I assume is always the 4th token when splitting the filename at the "_" character.
The inner FOR loop creates the folder and moves the file. Folder creation may fail when the folder already exists, "2>NUL" ensures that no error shows up on the screen in this case.

The source directory here is "XML\" the target directory is "XML2\" relative to the position of the batch file. This is to make sure that the outer FOR loop doesn't process the just moved files.

There are "echo." in front of the MD and the MOVE command, so you can see what the script will do first without actually moving the files.

Make sure you backup your XML files before running the batch.

DOS IT HELP? :wink:

Posted: 29 Apr 2008 16:46
by prashob12
Thanks for the reply this time :D

I tried to run the batch file by keeping it outside xml folder.

but in vein its was running but i didnt got any output.

I can make my Question more simple

I need to move xml files present in the subfolders in the output\XML depending on their file names to their respective folders.

1) create new folder inside the folder XML, namely DONE
2) There is subfolders inside DONE:

vt
cas
jurion
ls
pm


3) move the xml files from the subfolders present in XML TO DONE subfolders depending on their file names

i.e.

a) vgh_he_2006-03-14_12-a-2659-04_vt.xml in XML\vgh_he_2006-03-14_12-a-2659-04 will move to XML\DONE\vt

b) vgh_he_2006-03-14_12-a-2659-04_jurion.xml in XML\vgh_he_2006-03-14_12-a-2659-04 will move to XML\DONE\jurion

c) vgh_he_2006-03-14_12-a-2659-04_cas.xml in XML\vgh_he_2006-03-14_12-a-2659-04 will move to XML\DONE\cas

d) vgh_he_2006-03-14_12-a-2659-04_pm.xml in XML\vgh_he_2006-03-14_12-a-2659-04 will move to XML\DONE\pm

e) vgh_he_2006-03-14_12-a-2659-04_ls.xml in XML\vgh_he_2006-03-14_12-a-2659-04 will move to XML\DONE\ls


The same repeat for all the other subfolders in XML


I think what ever i explained is clear.


Thanks

Posted: 30 Apr 2008 18:55
by DosItHelp
prashob12,

May be it's not picking up the "current" directory. Try to use cd /d "%~dp0" in order to force the "current" directory to the directory with the batch file in it like this:

Code: Select all

@echo off
cd /d "%~dp0"
echo.The current directory is: "%cd%"
for /f "delims=" %%F in ('dir /b /s "XML\*.xml"') do (
    for /f "tokens=4 delims=_" %%D in ("%%~nF") do (
        echo.md XML2\%%D 2>NUL
        echo.move /Y "%%F" "XML2\%%D"
    )
)
pause


Let me know :wink:
Peter