Batch File move files base on part of their name - batch-file

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?

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.

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

Related

Batch - Rename multiple files to sequential numbers

I need to rename multiple files at once. Lets say I have a these files:
episode1.mkv
e1.mkv
s01e01.mkv
As you see, the file names have nothing in common.
How can I change the names of all the files to numbers (1.mkv - 2.mkv - 3.mkv ...) using batch.
I want the first file to be renamed to 1.mkv (no letters or spaces or anything else in the name) the second file to be renamed to 2.mkv, and so on.
I've looked around the internet a lot and I still didn't find anything that does exactly this.
TIA
Batch file version
#echo off
setlocal ENABLEDELAYEDEXPANSION
set/a fileNum = 1
for %%f in (*.mp4) do (
ren %%~nf%%~xf !fileNum!%%~xf
set/a fileNum += 1
)
GUI version
FreeCommander can do this for you.
Run FreeComander
Navigate to the folder.
Select all the files you want to rename.
Hit F2
The rename dialogue will appear. To rename all files names to be numeric do this:
Click Rename to make the change.
I suppose you could make a FOR /L loop with a limit of the amount of files in your directory, feed it a DIR /B |FINDSTR .mkv line by line renaming each file to an index variable you keep. Since you are going to name them into just numbers your DIR command will spit the same movie back as the first item over and over so maybe write the output of DIR /B|FINDSTR .mkv to a text file and work off of that? Or move the file you rename to another folder?
Once you make a rough project we could help you refine it? Or use FreeCommander and make a batch to do the rest you want to do after that.

CMD IF statement with IMAGE Dimmensions

So I need CMD if statement script to move files to certain folders depending on image name. In other words, script needs to check image file name: 123456_large.jpg and move it to folder LARGE. But once file is moved to this folder it needs to be renamed to: 123456.jpg instead of 123456_large.jpg...
So if statement finds *_LARGE.jpg, move to LARGE folder and rename filename to delete _LARGE.
Anyone any ideas?
Assuming all the files are in one common folder and the large directory also exists in that common folder you could use this.
#echo off
FOR /F "tokens=1* delims=_" %%G IN ('dir /a-d /b *.jpg') DO (
IF /I "%%~nH"=="large" move "%%~G_%%~H" "large\%%~G%%~xH"
)
So essentially what this code does:
Gets a listing of all jpg files in the current folder the batch file is in.
Splits the file name at the first underscore and assigns the first part of the file name to the variable %%G and the rest of the file name to %%H.
Using the FOR variable modifiers, it then checks if the second part of the filename without the extension is equal to large. If it is, it then moves the file and renames it at the same time, again using the FOR variable modifiers.
If you want to read about the FOR modifiers open up a cmd prompt and type: FOR /?
It is the very last section of the help file.
This worked absolutely perfect! Now a few improvements... Is there a way to do the same thing a previous script, but if file is named: 123456_ALT1_large.jpg (or ALT2, ALT3, ALT4)?

Windows Batch File - Move files with same file name but different extension

I have written an small batch file that moves all content from one folder to another.
This works fine. However, the source folder contains two types of files. One of the types is .doc and the other is .xml. Both files have the same name. But sometimes, one of the files (either .doc or .xml) is missing.
#echo off
move /y "\\networklocation\folder\folder\*.*" "M:\localfolder"
The question is how to make my script move only couples of .doc and .xml files that have the same name. For example, the source contains 1.doc, 2.doc and 1.xml. The script should only move 1.doc and 1.xml. 2.doc should stay in the source folder.
I've looked for this particular problem but have not found really anything.
Try this:
#echo off &setlocal
for %%i in ("\\networklocation\folder\folder\*.doc") do (
if exist "%%~dpni.xml" (
move /y "%%~i" "M:\localfolder"
move /y "%%~dpni.xml" "M:\localfolder"
)
)

Recursively move all files to one-upper directory

So here are the questions:
I have a folder, let's say C:\myFolder, and in this directory, I have many subfolders, and in each of this subfolders, I have exactly one folder, that contains pdf files, so my file structure looks something like this: C:\myFolder\someFolderInMyFolder\theOnlyFolderInThisFolder\*.pdf, now I want to move all these pdfs one level up, such that it will be like this: C:\myFolder\someFolderInMyFolder\*.pdf. Are there any command line commands, or scripts (that can be executed by Cygwin) that will help me with this?
What could complicate the situation is that, I have manually move some files one level up by myself, so it will help if there is a check condition.
I have some .zip files that the name are generated by computers, in the format of mm/dd/yy/fileIndex.zip, and the fileIndex is like No.001, for example. When I upload the extracted folders to Dropbox, and view the files on my iPad, it looks weird because the full folder name can not be displayed completely, so I want to rename each folder to someIndex, in the above example, from No.001 to 001, so same question here: any command or shell scripts?
You can move all PDFs up one level with a slightly modified version of what #Endoro suggested:
#echo off
for /r "C:\myFolder" %%f in (*.pdf) do move "%%~ff" "%%~dpi.."
However, there is no generic way for the script to distinguish files you already moved from files that have yet to be moved. It might be best if you undid your manual moves. Otherwise you'll have to find some distinguishing feature or check each name against a list of names, e.g. like this.
You can rename files like this:
#echo off
setlocal EnableDelayedExpansion
for /r "C:\myFolder" %%f in (No.*.zip) do (
set name=%%~nxf
set name=!name:No.=!
ren "%%~ff" "!name!"
)
endlocal
FTR, I somehow doubt that you really have files with names like mm/dd/yy/fileIndex.zip. Forward slashes are not valid characters for file names in Windows.

Referring to the contents of a wildcard as a variable in a batch file

I am trying to write a batch file to copy a large number of files. I want to be able to take the file and move it to a specific folder based on its file name.
For example, I have a directory structure like this:
C:/
FolderA
File1.xyz
File2.xyz
FolderB
File3.xyz
I would like to have a batch file that looks for all *.xyz files and copies them each to a folder according to their filename. So the above files would end up in the following directories.
File1.xyz gets copied to D:/FolderA/File1/File1.xyz
File2.xyz gets copied to D:/FolderA/File2/File2.xyz
File3.xyz gets copied to D:/FolderB/File3/File3.xyz
I know this should be possible using a FOR loop in a batch file, but I do not know how to take the text replaced by the wild card and use it as a variable (so I can create a folder with the same name.)
for /R C:\ %%f in (*.xyz) do (
if not exist D:%%~Pf%%~Nf md D:%%~Pf%%~Nf
copy %%f D:%%~Pf%%~Nf/%%~NXf
)
The FOR variable modifiers give the info you need:
%%~D Expands to a Drive letter only.
%%~P Expands to a Path only, including an ending backslash.
%%~N Expands to the Name only.
%%~X Expands to the eXtension only.
Type FOR /? for further details.
Perhaps you need to copy the directory structure first with:
XCOPY C:\ D:\ /T

Resources