0

I've been searching for a solution and have found similar solutions but they have either been with fixed length file names, or been used to sort files within a folder, rather than moving them.

What I'm attempting to do is when a staff member places, for example, a file in S drive named:

Bloggs Joe AHC20410 Student Forms.pdf

The batch file (run at certain times of the day) will move the files to the H drive, into the folder:

H:\Student Records Current\Bloggs Joe AHC20410

So, just for example S Drive could look like this when the batch file is run:

Bloggs Joe AHC20410 Student Forms.pdf
Bloggs Joe AHC20410 Evidence.jpg
Doe Jane AHC31010 Workbook.doc
Doe Joe AHC20410 Images.png

Any help will be greatly appreciated!

  • See `move /?`. Enclose paths with spaces in quotes. Type `help` for a list of commands. See http://stackoverflow.com/questions/31820569/trouble-with-renaming-folders-and-sub-folders-using-batch for some conventions at command prompt. –  Feb 05 '16 at 05:32

1 Answers1

0
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%d IN (
  'dir /b /ad "%destdir%\*ahc*" 2^>nul ^|findstr /i /r /c:"AHC[0-9][0-9][0-9][0-9][0-9]"'
  ) DO (
 FOR /f "delims=" %%a IN (
  'dir /b /a-d "%sourcedir%\%%d*"  2^>nul ^|findstr /i /r /c:" AHC[0-9][0-9][0-9][0-9][0-9] "'
 ) DO (
  ECHO(MOVE "%sourcedir%\%%a" "%destdir%\%%d\"
 )
)

GOTO :EOF

You would need to change the settings of sourcedir and destdir to suit your circumstances.

The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)

assumed that the destination directories already exist.

Essentially, find all of the directorynames containing ahc, and filter for ahc followed by 5 numerics for good measure. Assign these strings to %%d.

Then for each %%d found, look for source files that start %%d and contain the string ahc followed by 5 numerics and both preceded and succeeded by a space.

Bang the names together and show the result...

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thank you very much @Magoo for taking the time to write the code, my batch file skills are pretty rusty. I was confused for a bit when the code found the source and destination, but didn't copy... But eventually noticed a missing ')'. Works perfectly! – Henrich Consulting Feb 09 '16 at 01:13