批处理文件可根据名称移动文件而无需创建新文件夹 [英] Batch file to move files based on name w/o creating new folders

查看:164
本文介绍了批处理文件可根据名称移动文件而无需创建新文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据文件名将文件从一个目录移动到另一个目录.文件采用12345 123456.pdf格式,其中2个字符串长度可以变化.它们之间始终会有一个空格,并且它们始终是PDF文件.

I need to move files from a directory to another directory based on the files name. The files are in the format of 12345 123456.pdf where the 2 string lengths can vary. There will always be a space between them though and they are always PDF files.

目标目录是文件名的第一个字符串,即"SAME NAME"(例如,10003075 3000101012.pdf目标目录将是10003075).

The destination directory is the SAME NAME as the first string of the filename (ex. 10003075 3000101012.pdf destination directory would be 10003075).

如果目标目录不存在,我不要创建它.该文件应保留在初始目录中.

If the destination directory does not exist I DO NOT want it created. The file should be left in the initial directory.

文件结构如下:

Main Folder
  |
  Destination Directories
  Files waiting to be moved Directory
    |
    Batch file 

因此,批处理文件将必须检查pdf文件所在的目录,然后检查其上方的目录中与文件名中第一个字符串相对应的目录,然后仅将该文件移动到该目录(如果存在)否则将文件保留在原处.

So the Batch file would have to check the directory it is in for pdf files, then check the directory above it for the directory corresponding to the first string in the file name, then move that file to that directory only if it exists else leave the file where it is.

我已经对stackoverflow提出了一些建议,但是我看到的所有内容都会创建目录,或者如果目录不存在,则将文件移至主目录.

I have worked with some suggestions on stackoverflow but everything I have seen will either make the directory or move the file to the main directory if the directory is not there.

推荐答案

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
PUSHD "%sourcedir%"
FOR /f "tokens=1*delims= " %%a IN (
 'dir /b /a-d "* *.pdf" '
 ) DO (
 IF EXIST "..\%%a\." (ECHO(MOVE "%%a %%b" "..\%%a\") ELSE (ECHO(Leave "%%a %%b")
)
popd

GOTO :EOF

您需要更改sourcedir的设置以适合您的情况.可以将其分配给%~dp0.

You would need to change the setting of sourcedir to suit your circumstances. Assigning it to %~dp0 is a possibility.

给出(%sourcedir%的部分dir/a:d)

Given (partial dir /a:d of %sourcedir%)

20/01/2015  09:49    <DIR>          one
20/01/2015  09:49    <DIR>          t w o
20/01/2015  09:50    <DIR>          1232
20/01/2015  09:50    <DIR>          1234

和pdf来源目录

u:\ sourcedir \ t w o的目录

Directory of u:\sourcedir\t w o

20/01/2015  09:50                 0 dum myfile2.pdf
20/01/2015  09:50                 0 1231 54321.pdf
20/01/2015  09:50                 0 1232 54321.pdf
20/01/2015  09:50                 0 1233 54321.pdf
20/01/2015  09:50                 0 1234 54321.pdf
20/01/2015  09:50                 0 1235 54321.pdf
               6 File(s)              0 bytes

这产生

Leave "dum myfile2.pdf"
Leave "1231 54321.pdf"
MOVE "1232 54321.pdf" "..\1232\"
Leave "1233 54321.pdf"
MOVE "1234 54321.pdf" "..\1234\"
Leave "1235 54321.pdf"

所需的MOVE命令仅被ECHO用于测试目的. 验证命令正确无误后,将ECHO(MOVE更改为MOVE以实际移动文件.附加>nul以禁止显示报告消息(例如1 file moved)

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)

Leave消息及其关联的else子句当然是可选的.

The Leave message and its associated else clause would of course be optional.

这篇关于批处理文件可根据名称移动文件而无需创建新文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆