Batch to move files based on a common prefix - batch-file

I have a folder with many files, most are file type .nib with some being .m3u files.
I want to create a folder for each m3u file with the same folder name as the m3u, and then move the m3u file as well as any nib file that starts by the full m3u name in that folder.
I tried to write a batch command but it only created the folder and moved the m3u but not the nib files sharing its prefix.
Here is what I have:
for %%i in (*.m3u*) do md "%%~ni" & move "%%i" "%%~ni" & move "%%i*.nib" "%%~ni"
I think the problem is with the syntax of %%i*.nib, I need something to convey any file that starts with the full name of that m3u, followed by an unknown number of characters, then extension nib.
Could someone please help?

Related

batch find file, replace with another file, that is supplied

I need help creating a batch file to find multiple files named the same thing in 100's of files and replace the with a different file...
Alternatively, is there a program that will do this if I select the 2 different files that will search and replace them...
Find file from computer Pairing = High School DxD - Kuroka.png
Replace file with Pairing = High School DxD - Kuroka (F).jpg
Since you never provided the basic information such as What directory you wish to start a search from or You wish to convert or just rename, I will assume you wish to search your photo's folder (Including sub-directories) and simply rename file.jpg to file.png.
To search photo's and all its directories we can use the FOR /R to search for all .jpg files and DO to address the action. The script bellow will do the following.
Search and replace all .jpg extensions to .png in the photo folder directory
Batch Script:
#ECHO ON
#CD C:\Users\%username%\Pictures
For /r %%A In (*.jpg *.jpeg) Do ren "%%A" "*.png"
goto :eof

xcopy - copy files from the containing directory to directories on folder drag

I have a .bat file sat in a directory with a number of resource .resx files.
When I drag a folder on to the .bat file I want to be able to copy the files that begin with the desired text (e.g. Web.de.resx, Web.fr.resx etc.), in that directory, to the corresponding directories in the desired folder.
I have tried the following, but I'm getting error: File not found - Web*
xcopy /i "Web*" "%~1\src\Resources\web"
You need to specify the directory in which your .bat should look for Web* files.
You can do this by using the "magical" %~dp0 variable, with ~d is the drive and ~p is the path (without drive) where you are executed, so, ~dp will be the drive and path.
with xcopy /i "%~dp0\Web*" "%~1\src\Resources\web" it should work better.

Batch File move files base on part of their name

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

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"
)
)

Determine file name in subfolder from batch file

I have a directory structure like this:
- Root
- Versioned deployment folder
- config file
The "Versioned deployment folder" name varies with each version of the application. The config file name stays the same.
I'd like to write a batch file that opens a program on the config file. The batch file should stay the same regardless of the name of the intermediate folder. The config file will be the only file with its name under the Root.
How do I get the path to the config file consistently from within the batch file?
It seems like there's some missing bit of information here... The batch file simply must have some idea of what the versioned folder name is. Is there a rule? Do you always want the latest versioned folder? If so, how do you name the deployment versions?
Update: So if you only have one sub-folder (the versioned folder) and you want a fixed batch file to always execute some call on a fixed file name in that sub-folder, you can do this:
#for /f "tokens=* delims= " %%a in ('#dir /ad /b') do #set FOLDER=%%a
echo %FOLDER%
call notepad %FOLDER%\test.config
Obviously change test.config to be the fixed name of the folder.
Does that meet your needs...?
You could use dir /s /b configfile to get the file path. Store this in a variable and you can strip out the file name using %variable:~0,-x%. x is the file name length.
Hope this helps.

Resources