[英]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. 背景故事:我有一个excel文件,保存时会生成两个文件,一个.PDF和一个.xlsm。 These files are saved into a 'To-Process' folder.
这些文件保存在“To-Process”文件夹中。
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' 它们以以下命名约定命名:公司名称 - 日期,例如'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/ 处理:每周一次我通过公司名称手动将它们移动到单独命名的文件夹中,例如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. 当我决定处理时,我可以在文件夹中有超过50个文件。
Help on this is greatly appreciated, I hope I have provided sufficient information to see if this is feasible. 非常感谢您的帮助,我希望我提供了足够的信息,看看这是否可行。
Many thanks, Ash 非常感谢,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? ps作为旁注,我假设我无法在移动之前将PDF文件作为电子邮件附件发送? eg execute batch file, send PDF's via email to pre-defined email address, then move both 'pdf' and 'xlsm' to relevant folders.
例如,执行批处理文件,通过电子邮件将PDF发送到预定义的电子邮件地址,然后将“pdf”和“xlsm”移动到相关文件夹。 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. 有批处理/ VBS脚本可以发送带有附件的电子邮件。
Google for them, or use Blat
or Sendmail
. 谷歌为他们,或使用
Blat
或Sendmail
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.