Move to existing directory based on filename

August 20, 2010 at 10:17:57
Specs: Windows XP

Let me preface this by saying that I know nothing about computer programming.

I need a script that can pickup files from a dump folder (i.e, J:\ScanFiles\Fax dump) and move them to different folders (which already exist), based on the ID portion of the filename.

For example, a file in J:\ScanFiles\Fax dump would be named "1056_7_22_10.pdf" "1056" is the ID; note that this number could potentially be 6 figures or perhaps greater. So there could also be a "1056_7_23_10.pdf" etc. in Fax dump. These files should be moved to J:\ScanFiles\1056 This directory doesn't need to be created as it already exists.

So, as another example, a file in J:\ScanFiles\Fax dump named "51608_7_23_10.pdf" should be moved to J:\ScanFiles\51608 Again the destination directory already exists, the file just needs to be moved.

Thanks so much for your help!


See More: Move to existing directory based on filename

Report •

August 20, 2010 at 20:06:22
Try this, it's not fully tested, leave that to you. Change the Copy to Move when you are satisfied with its performance.

@echo off
setlocal enabledelayedexpansion

set nbr=0

set  input=j:\scanfiles\fax dump\
set output=j:\scanfiles\

pushd %input%||echo PUSHD failed - job terminated&&exit/b

for /f "tokens=*" %%1 in ('dir /b "%input%*.pdf"') do (
    set filename=%%1

        for /f "tokens=1-4 delims=_" %%A in ("!filename!") do (
            set dirname=%%A\

            copy !filename! !output!!dirname!>nul&& set /a nbr+=1


echo Files copied = %nbr%
exit /b

Report •
Related Solutions

Ask Question