Move files depending on content

March 11, 2010 at 03:59:19
Specs: Windows XP
Hi,

Right, I have a directory containing files and folders, the files have various names and the content of the files is as follows:
ZHV|PM00167009|D0187001|V|XXXX|X|XXXX|20091218071327||||OPER|
384|32B000NNN0018277|554NN508|06|200NN113|134N00|-N.08||
385|01|2N386.0|
385|02|1N4.0|
ZPT|PM00167009|3||1|20091218071327|

What I want to do is go through all the files in the directory and based on the 3rd field on the ZHV line (in this case D0187001) move that file into the D0187001 folder and if that folder doesnt exist then create it.

I have attempted to get it working but the batch file handels 1 or 2 files the exits and I cant understand why as there are 30+ files to process.

Here is the batch file I have so far:
@echo off
set PROC=elec_in
set FILE_DIR=M:\tmp
set WORKING_DIR=%FILE_DIR%\working
set DIR_LIST=%WORKING_DIR%\%PROC%.lst
set HEADER_TMP=%WORKING_DIR%\hdr.tmp

dir /a:-d /B %FILE_DIR% > %DIR_LIST%
for /F %%F in (%DIR_LIST%) do (
FINDSTR /C:"ZHV" %FILE_DIR%\%%F > %HEADER_TMP%
for /F "tokens=3 delims=|" %%i in (%HEADER_TMP%) do call :PROCESSCMD %%i, %%F
:PROCESSCMD
set type=%1
set name=%2
echo FLOW_TYPE - %type% FILE_NAME - %name%
IF EXIST %FILE_DIR%\%type%\NUL goto MOVE_FILE
mkdir %FILE_DIR%\%type%
goto MOVE_FILE
:MOVE_FILE
move %FILE_DIR%\%name% %FILE_DIR%\%type%\%name%
if "%type%" == "" goto PAUL
:PAUL
echo next
)

Thanks in advance

Paul


See More: Move files depending on content

Report •

#1
March 13, 2010 at 20:15:40
if you're still interested, can you post contents of %dir_list%?
also if you're getting any errors, the content of those msgs.
(do filenames contain spaces in dir_list?)
this thread fell back into "moribund" territory...

Report •

#2
March 15, 2010 at 01:45:27
Hi,

Thanks for your reply, as requested here is the contents of the %dir_list%:

V11017202.usr
V11018229.usr
V11023019.usr
V11028599.usr
V11032905.usr
V11038392.usr
V11042301.usr
V11045064.usr
V11048558.usr
V11052615.usr
V11054846.usr
V11070342.usr
V11077191.usr
V11081218.usr
V11082703.usr
V11092432.usr
V11107622.usr
V11129870.usr
V11138746.usr
V11142053.usr
V11142557.usr
V11144699.usr
V11167583.usr
V11175677.usr
V11180156.usr
V11207112.usr
V11213572.usr
V11231644.usr
V11237598.usr
V11250946.usr
V11251062.usr
V11251063.usr
V11251177.usr
V11251178.usr
V11251179.usr
V11251180.usr
V11251186.usr
V11251187.usr
V11251188.usr
V11251189.usr
V11251190.usr
V11251191.usr
V11251192.usr
V11251193.usr
V11251194.usr
V11251195.usr
V11251196.usr
V11251197.usr
V11251198.usr
V11251199.usr
V11251200.usr
V11251201.usr
V11251202.usr
V11251203.usr
V11251204.usr
V11251205.usr
V11251206.usr
V11251207.usr
V11251208.usr
V11251209.usr
V11251210.usr
V11251211.usr
V11251212.usr
V11251213.usr
V11251214.usr
V11251215.usr
V11251216.usr
V11251217.usr
V11251326.usr
V11251327.usr
V11251328.usr
V11251329.usr
V11251330.usr
V11251331.usr
V11251332.usr
V11251333.usr
V11251334.usr
V11251335.usr
V11251421.usr
V11251433.usr
V11251439.usr
V11251859.usr
V11251990.usr
V11252412.usr
V11252414.usr
V11252415.usr
V11252598.usr
V11252837.usr
V11252881.usr
V11252882.usr
V11252883.usr
V11252922.usr
V11252923.usr
V11253289.usr
V11253291.usr
V11253297.usr
V11253298.usr
V11253313.usr
V11253316.usr
V11253338.usr
V11253339.usr
V11253345.usr
V11253360.usr

Also there is no errors thrown by the script.

Thanks

Paul

Thanks and Regards

Paul


Report •

#3
March 15, 2010 at 13:20:47
it looks like you need to move :proc outside the for-loop, change:
for /F %%F in (%DIR_LIST%) do (
FINDSTR /C:"ZHV" %FILE_DIR%\%%F > %HEADER_TMP%
for /F "tokens=3 delims=|" %%i in (%HEADER_TMP%) do call :PROCESSCMD %%i, %%F
(also might want to remove redunant separator) to:
for /F %%F in (%DIR_LIST%) do (
FINDSTR /C:"ZHV" %FILE_DIR%\%%F > %HEADER_TMP%
for /F "tokens=3 delims=|" %%i in (%HEADER_TMP%) do call :PROCESSCMD %%i %%F
)
goto :eof
:processcmd
...

Report •

Related Solutions

#4
March 30, 2010 at 04:41:23
Hi,

Thanks for your reply although the script still failed after processing just one file, however after taking a 2 week break from it I have cracked it, it's now doing exctly what I want and here is how I done it.

dir /a:-d /B %FILE_DIR% > %DIR_LIST%
IF ERRORLEVEL 1 goto ERROR

for /F %%F in (%DIR_LIST%) do (
FINDSTR /C:"ZHV" %FILE_DIR%\%%F > %HEADER_TMP%
for /F "tokens=3 delims=|" %%i in (%HEADER_TMP%) do (
echo FLOW_TYPE - %%i FILE_NAME - %%F
IF EXIST %FILE_DIR%\%%i\NUL (
echo "Moving %%F to %FILE_DIR%\%%i"
move %FILE_DIR%\%%F %FILE_DIR%\%%i\%%F
echo "Moved %%F to %FILE_DIR%\%%i"

) ELSE (
echo "Creating %FILE_DIR%\%%i directory"
mkdir %FILE_DIR%\%%i
echo "Moving %%F to %FILE_DIR%\%%i"
move %FILE_DIR%\%%F %FILE_DIR%\%%i\%%F
echo "Moved %%F to %FILE_DIR%\%%i"
)
)
)

if ERRORLEVEL 1 (
goto ERROR
) ELSE (
goto SUCC
)

:ERROR
echo "Script has failed"
echo "Return code %errorlevel%"
echo %errorlevel%
goto :eof

:SUCC
echo "Return code %errorlevel%"
echo %errorlevel%
goto :eof

Thanks and Regards

Paul


Report •

Ask Question