Solved Move multile pdfs to different folders

February 25, 2014 at 15:13:12
Specs: Windows 7
Move pdfs to same named folder
I have about 1000 pdfs each beginning with 6 digits and I want to move the pdf to the folder with the same 6 digits. For example

312574_xxxx.pdf
312574 is the folder name

How can I move 312574_xxxx.pdf to the 312574 folder and then repeat this command for each of the pdfs and folders I have.


See More: Move multile pdfs to different folders

Report •

#1
February 25, 2014 at 20:00:53
@echo off & setlocal enabledelayedexpansion
for %%a in (*.pdf) do (
set z=%%a
set z=!z:~0,6!
:1
rem move %%a !z!\||md !z!&&goto :1
echo move %%a !z!\
)
::=== end batch, not tested, safety is ON, remove "rem" to arm the weapon.

If the subdir does not exist, batch attempts to create one. If perchance a file exists that has the subdir name, theoretically the MD should fail and the batch skips to the next PDF.
Forfiles or powershell are better options overall.

message edited by nbrane


Report •

#2
February 26, 2014 at 09:09:58
✔ Best Answer
@nbrane,

GOTO statement jumping inside a block sequence, i.e. a set of statements embraced by brackets, is not allowed since it disrupts the flow sequence.
So your script must be rewritten as

@echo off & setlocal enabledelayedexpansion
for %%a in (*.pdf) do (
  set z=%%a
  set z=!z:~0,6!
  if not exist !z! md !z!
  move %%a !z!\
)


Report •

#3
February 26, 2014 at 13:43:13
Yeh, that was a very amature-ish mistake on my part! Thanks Ivo. My original worry was possibly overwriting an existing file, but the backslash prevents that.

Report •

Related Solutions

#4
February 26, 2014 at 17:12:46
Thank you for your response and assistance - this made it extremely easy to do instead of having to move each individual pdf one by one.

Report •

Ask Question