简体   繁体   English

批处理文件根据文件名创建目录,将类似文件移动到新目录

[英]batch file to create directory based on file name, move like files to new directory

I have hundreds of files in a directory that all have different names but some of those are related.我在一个目录中有数百个文件,它们都有不同的名称,但其中一些是相关的。

The files are mostly.AI and PDF and are all names like this:文件大多是.AI和PDF,都是这样的名字:

  • FILE1_retail_xxx.ai FILE1_retail_xxx.ai
  • FILE1_label_xxx.ai FILE1_label_xxx.ai
  • FILE2_retail_xxx.ai FILE2_retail_xxx.ai
  • FILE2_label_xxx.ai FILE2_label_xxx.ai
  • FILE3_retail_xxx.ai FILE3_retail_xxx.ai
  • FILE3_label_xxx.ai FILE3_label_xxx.ai

What I want to do is have a batch file create directories for FILE1, FILE2, FILE3, etc. and move all files that start with FILE1, FILE2, FILE3, etc. into those newly created directories.我想要做的是有一个批处理文件为 FILE1、FILE2、FILE3 等创建目录,并将所有以 FILE1、FILE2、FILE3 等开头的文件移动到这些新创建的目录中。

I have used this, but it just makes a DIR for the whole filename.我已经使用了它,但它只是为整个文件名创建了一个 DIR。

@echo off
for %%i in (*) do (
 if not "%%~ni" == "organize" (
  md "%%~ni" && move "%%~i" "%%~ni"
 )
)

This doesn't do what I need, can anyone help massage this into what I need or give me a new batch file to try?这不能满足我的需要,任何人都可以帮我把它按摩成我需要的东西或给我一个新的批处理文件来尝试吗?

Thanks!谢谢! This will save me countless hours of manual work!这将节省我无数小时的体力劳动!

You need to split the filename by _ with another for /f loop:您需要使用另一个for /f循环将文件名拆分为_

@echo off
for %%i in (*) do (
  if not "%%~ni" == "organize" ( 
    for /f "delims=_" %%a in ("%%~ni") do (
      md "%%a" 2>nul 
      move "%%~i" "%%~a\" 
  )
)

2>nul suppresses the error message in case the folder already exists. 2>nul在文件夹已经存在的情况下抑制错误消息。

Please consider using for %%i in (*_*) do ( or for %%i in (*_*_*.ai *_*_*.pdf) do ( - according to your actual needs/file names to pre-select files to be moved.请考虑使用for %%i in (*_*) do (for %%i in (*_*_*.ai *_*_*.pdf) do ( - 根据您的实际需要/文件名来预先-选择要移动的文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理文件以递归方式查找名为特定名称的文件并移至目录 - Batch file to recursively find files named a specific name and move to a directory 批处理:如何根据文件名将文件复制到目录 - BATCH: How can I copy files to directory based on file name 根据部分文件名批量创建文件夹并将文件移动到文件夹 - Batch create folder based on partial file name and move files to folder 批量创建基于部分文件名的文件夹并将文件移动到该文件夹​​中 - Batch create folders based on part of file name and move files into that folder Windows批处理文件:根据目录名称从目录中选择文件 - Windows batch file: choosing a file from a directory based on directory name 批处理文件以在文件夹中创建新的子文件夹,将文件移动到目录中所有文件夹的新创建的子文件夹 - batch file to create new subfolder in folder, move file to newly created subfolder, for all folders in directory 批处理文件使用多个文本文件将文件移动到另一个目录 - Batch File Move files using multiple text files into another directory 如何使用批处理脚本创建没有文件类型扩展名的文件夹名称并移入创建的子目录? - How to create folder name without extension of file type using batch script and move in created sub directory? 创建一个批处理 (.bat) 文件以移动到特定目录和 output (echo) 列出的所有文件? - Create a batch (.bat) file to move to a specific directory and output (echo) all files listed? 用于将文件移动到新目录的批处理命令 - Batch command to move files to a new directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM