Solved

Batch file that will move PDFs to corresponding folders

Posted on 2014-11-28
8
273 Views
Last Modified: 2014-12-01
Hello,

Our financial institution is using a system that generates client PDFs in a specific format. For example, the system we use exports PDFs in location \\serverA\clientPDFs\ as this format:

1455-Douglas T Thompson-2013Archivek1Package.pdf
4785-Cheri Lockson-2013Archivek1Package.pdf
7899-Thomas Pete-2012Archivek1Package.pdf
etc... (we have hundreds of them waiting to be moved)

but all clients of different types are in 1 folder instead of its own assigned client folder.

I need a batch file that will migrate the clients above to its corresponding client ID. An example of its final result would be:

\\serverB\client\1455\1455-Douglas T Thompson-2013Archivek1Package.pdf\
\\serverB\client\4785\4785-Cheri Lockson-2013Archivek1Package.pdf\
\\serverB\client\7899\7899-Thomas Pete-2012Archivek1Package.pdf\


anything will help, thanks in advance!
0
Comment
Question by:vlsllp
8 Comments
 
LVL 20

Expert Comment

by:dsacker
ID: 40470687
Put this in a .bat file, and test it:
@echo off

for /f %%a in ('dir /b \\serverA\clientPDFs\*.PDF') do (
    call:MoveThisFile %%a
)
goto:EOF

:MoveThisFile

    set file=%1
    set node=%file:~0,4%

    rem Comment out the following two lines when you're ready to run this for real

    echo if not exist \\serverB\client\%node% mkdir \\serverB\client\%node%
    echo move \\serverA\clientPDFs\%file% \\serverB\client\%node%\.

    rem Remove the "rem" comments from these next two lines when you're ready to run this for real

    rem    if not exist \\serverB\client\%node% mkdir \\serverB\client\%node%
    rem    move \\serverA\clientPDFs\%file% \\serverB\client\%node%\.

    goto:EOF

Open in new window

0
 
LVL 24

Expert Comment

by:NVIT
ID: 40470688
Try this:

@echo off
for %%a in (*.pdf) do (call :DoIT %%a)
goto :EOF

:DoIt
echo.
set IDA=%1
set ID=%IDA:~0,4%
md %ID%	
move "%1" %ID%
)

Open in new window

0
 

Author Comment

by:vlsllp
ID: 40470815
We appreciate the quick reply!

i tried inputting your code into a .bat file,

Prior testing this on my server I created 2 folders on my desktop: "%USERPROFILES\Desktop\serverA\" and "%USERPROFILES\Desktop\serverB\

 here's what I have so far, but didn't seem to work. thanks again

@echo off

for /f %%a in ('dir /b %USERPROFILE%\Desktop\serverA\*.PDF') do (
    call:MoveThisFile %%a
)
goto:EOF

:MoveThisFile

    set file=%1
    set node=%file:~0,4%

    rem Comment out the following two lines when you're ready to run this for real

    echo if not exist %USERPROFILE%\Desktop\serverB\%node% mkdir %USERPROFILE%\Desktop\serverB\%node%
    echo move %USERPROFILE%\Desktop\serverA\%file% %USERPROFILE%\Desktop\serverB\%node%\.

    rem Remove the "rem" comments from these next two lines when you're ready to run this for real

    if not exist %USERPROFILE%\Desktop\serverB\%node% mkdir %USERPROFILE%\Desktop\serverB\%node%
    move %USERPROFILE%\Desktop\serverA\%file% %USERPROFILE%\Desktop\serverB\%node%\.

    goto:EOF
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 20

Expert Comment

by:dsacker
ID: 40470845
What kind of error message(s) did you get?
0
 
LVL 23

Assisted Solution

by:Eirman
Eirman earned 100 total points
ID: 40470846
This little utility will do what you want (and other stuff as well) without coding ...
http://www.robobasket.com/create-folder-using-part-of-filename.html
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40470866
Maybe you need to wrap these in quotes?
%USERPROFILE%\Desktop\serverB\%node%
%USERPROFILE%\Desktop\serverA\%file%

Open in new window

i.e.
"%USERPROFILE%\Desktop\serverB\%node%"
"%USERPROFILE%\Desktop\serverA\%file%"

Open in new window


Also, does that ending "." need to be removed?
move \\serverA\clientPDFs\%file% \\serverB\client\%node%\.

Open in new window

0
 
LVL 20

Accepted Solution

by:
dsacker earned 400 total points
ID: 40470880
I have amended the code a little, as follows, where you can set your source an target directories up front:
@echo off

    setlocal

    set source=\\serverA\clientPDFs
    set target=\\serverB\client
    set files=*.PDF

    for /f "delims=^|" %%a in ('dir /b "%source%\%files%"') do (
        call:MoveThisFile %%a
        if errorlevel 1 goto:EOF
    )

    goto:EOF

:MoveThisFile

    set file=%*
    set node=%file:~0,4%

    if not exist "%target%\%node%" mkdir "%target%\%node%"
    if errorlevel 1 goto:EOF

    move "%source%\%file%" "%target%\%node%\."
    goto:EOF

Open in new window

I changed the code recently (a few hours after posting this, to wrap double-quotes around the source and target, just in case you had spaces in the file or directory names. I also added some error checking. Tested it with some test files I created, and it seems to work okay.
0
 
LVL 20

Expert Comment

by:dsacker
ID: 40471098
FYI, vlsllp, I changed the code a little (above), adding double-quotes in case of any spaces and adding error checking. I tested it with files I created,and it seems to work okay.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Non-recursive backtracking, using a stack 1 126
Basic Java Case or If-Else statement... 3 50
x-bar in Google Sheets 2 50
Cygwin - GNU GPL License 1 22
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now