简体   繁体   中英

Moving files to folders based on Name and Course Code with Batch File

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!

@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 ECHO ed 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM