Batch File move files base on part of their name

本秂侑毒 提交于 2019-12-21 23:03:54

问题


Is it possible for a bat file to search through a folder and look at the file names and only move files with that name or part of that name in it? Then move them into a specified location. For example:

Parent Folder 
Arrow0273.text
Arrow0314.text
Spear083112.text
Spear0832.text 
Sheild087.txt
Sheild87.txt 

Move only the files with “Arrow” in their name into folder location “A”. 
ect... 

Thanks Guys! Edit: Found this but not sure if it is what I'm looking for, and to be honest, not sure how that code works. Move files to directories based on some part of file name?


回答1:


copy supports wild cards so all you need to do is:

copy Arrow* A




回答2:


Maybe something like this? I found this on here a while ago, and I use it all the time. It will move files into a folder based on the file name and type. If the folder doesn't exist, it will create the folder in the current location of the batch file. If the folder already exists, it will simply move it to that folder.

@echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.text') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~0,1!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!" >nul
endlocal
)

The ".pdf" in line 2 can be changed to specify the file type. You may use ".*" to move all file types, though this will also move the .bat file and folders.

The "~0,1!" in line 5 determines which characters are looked at to determine the folder names. The first number determines which character it begins looking at (0 is at the beginning, 1 is 1 character from the beginning, etc). The second number determines how many characters it looks at. If it was changed to 2, it will look at the first 2 characters in the file.

Currently it is set to only look at the first character and move only .text files. For the files in your example, it would move all of the "Arrow" files for a folder named "A", and all of the "Spear" files to a folder named "S". The "Shield" files would stay where they are, as their extension is .txt, not .text. If you changed ".text" to ".t*" it will move both the .txt and .text files into the "A" and "S" folders.



来源:https://stackoverflow.com/questions/16207587/batch-file-move-files-base-on-part-of-their-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!