批处理:将文件移到该文件夹​​的名称的第一个字母吗? [英] Batch: Move files to folder for first letter of the name?

查看:44
本文介绍了批处理:将文件移到该文件夹​​的名称的第一个字母吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有3个文件夹,A-G,H-T和U-Z.我希望根据名称的第一个字母,通过批处理将文件自动分类到这些文件夹中.有什么办法吗?谢谢!

I wanted to have 3 folders, A-G, H-T, and U-Z. I wanted files to automatically be sorted into these folders via batch based on the first letter of the name. Any way to do this? Thank you!

推荐答案

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir"
PUSHD "%sourcedir%"
FOR %%a IN (*) DO (
 IF /i "%%a" geq "a" IF /i "%%a" lss "h" ECHO(MOVE "%sourcedir%\%%a" "%destdir%\A-G\"
 IF /i "%%a" geq "h" IF /i "%%a" lss "u" ECHO(MOVE "%sourcedir%\%%a" "%destdir%\H-T\"
 IF /i "%%a" geq "u" (
  IF /i "%%a" lss "z" (ECHO(MOVE "%sourcedir%\%%a" "%destdir%\U-Z\"
  ) ELSE (
   SET "name=%%a"
   IF /i "!name:~0,1!"=="Z" ECHO(MOVE "%sourcedir%\%%a" "%destdir%\U-Z\"
  )
 )
)
popd

GOTO :EOF

您需要更改 sourcedir destdir 的设置以适合您的情况.

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

所需的COPY命令只是出于测试目的而 ECHO .确认命令正确无误后,将 ECHO(COPY )更改为 COPY ,以实际复制文件.nul 禁止显示报告消息(例如,复制 1个文件)

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

困难的部分是将名称与进行比较(大于Z).不幸的是, [ {"都不在这个位置上工作.如果认为 [ {都低于 A -甚至是数字...

The difficult part is comparing the name against (something greater than Z). Unfortunately, neither [ nor { work in this position. if regards both [ and { as lower than A - or even numerics...

因此,对于以 z 开头的文件名,请使用特定的测试.

Hence, for filenames beginning z, employ a specific test.

这篇关于批处理:将文件移到该文件夹​​的名称的第一个字母吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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