简体   繁体   中英

Windows batch - Move files to folders based on file name & extension

I am new to batch files although have searched thoroughly and found topics with a similar theme, although not covering what I precisely need.

Back story: I have an excel file which when saved generates two files, a .PDF and an .xlsm. These files are saved into a 'To-Process' folder.

They are named with the following naming convention: Company name - Date, so for example 'Daves Plumbing - 01-08-13.xlsm' 'Daves Plumbing - 01-08-13.pdf'

Processing: Once a week I manually move these into separately named folders by company name eg C:/documents/sales/excel/daves plumbing/ C:/dociuments/sales/pdf/daves plumbing/

I appreciate spaces in the company name don't help, but hopefully we could use the - as a delimiter?

Ultimately I am looking to create a batch file which will automate the processing above by stepping through the files in the folder, reading the company name and moving each file to the relevant folder based on the name and extension.

I can have upwards of 50 files in the folder at any one time when I decide to process.

Help on this is greatly appreciated, I hope I have provided sufficient information to see if this is feasible.

Many thanks, Ash

ps As a side note, I am assuming I could not incorporate sending the PDF files as an email attachment prior to moving them? eg execute batch file, send PDF's via email to pre-defined email address, then move both 'pdf' and 'xlsm' to relevant folders. This is not important, merely curious of the capabilities of batch processing.

EDITED to use different destination folders. Don't use a - in the batch filename if you plan to use it in the same folder.

Try this. It uses the first - in the filename as the delimiter, removes the last character which is a space, makes a directory if it needs it or not, and moves the files into the appropriate folder.

Filenames containing ! and % could be an issue.

@echo off
cd /d "c:\To-Process"
setlocal enabledelayedexpansion
for %%a in (*-*.*) do (
   for /f "delims=-" %%b in ("%%a") do (
     set "f=%%b"
       if /i "%%~xa"==".pdf" (
         md "C:/documents/sales/pdf/!f:~0,-1!" 2>nul
         move "%%a" "C:/documents/sales/pdf/!f:~0,-1!" >nul
       )
       if /i "%%~xa"==".xlsm" (
         md "C:/documents/sales/excel/!f:~0,-1!" 2>nul
         move "%%a" "C:/documents/sales/excel/!f:~0,-1!" >nul
       )
   )
)

There are batch/VBS scripts to send an email, with an attachment.
Google for them, or use Blat or Sendmail .

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