How to replace a word if it is in a file name? - cmd

I have multiple files
P_H_Home.png
P_Log.png
D_H_Car.png
I need to replace _H_ with _G_
And I want get
P_G_Home.png
P_Log.png
D_G_Car.png
How can i do this?
I am not very familiar with CMD/BAT And now I just want to rename all files It should look something like this but work
for %I in (*) do ren "%I" "%I"str:_H_=_G_

Related

How to batch rename files with two extensions?

I have a file conversion tool that converts the files correctly but just appends the desired file type extension to the old filename.
So example files are something like
file1.ext1.ext2
.
.
.
fileN.ext1.ext2
I thought it would be an easy fix - just use a wildcard operator on the ren command.
example:
ren \dir*.ext1.ext2 *.ext2
but the output remains .ext1.ext2. I feel like I'm close.
Any suggestions? I've seen suggestions to use push and pop but those seem unnecessary.
Thank you!
The * wildcard has a fair bit of compatibility handling related to DOS 8.3 names and whatnot. How did copying and renaming with wildcards work in MS-DOS?
One way to get around it is to use full names without wildcards in the rename operation:
#echo off
REM test file:
echo.>> "file1.foo.bar"
REM remove foo extension:
for %%A in (*.foo.bar) do for /F "eol=? delims=" %%B in ("%%~dpnA") do #ren "%%~fA" "%%~nB%%~xA"
(Change %% to % if not in a batch file)

Windows Batch script to rename files with it's folder name within a for loop

I have a bunch of .flv files in subdirs and I need to loop through them and rename it according to its path.
I was able to loop through them all, but I don't know how to split the path and rename it using batch script.
Here's what I have so far:
echo off
for /R %%F in (*.flv) do (
echo %%~pF
)
The "echo %%~pF" prints the path for the current file on the loop, something like this:
\folder\morefolders\activity\ NameThatIwant \Videos\
I tried spliting with "delims=\" on my for loop but I get only "echo off".
I tried other tutorials, read other questions on SO but none of them were renaming the files from a split string from the path of the file in the loop.
Could you guys help giving suggestions or direct me to any material that explains those %% codes?
Thanks.
I think you do not need to split the path, though you could do it using for /f "delims=NameInthePath tokens=1", where NameInthePath - is some word in the path and tokens= gives you the first part of the path separated by delims.
Actially, if you need to rename file name you need to use REN command. If you need to change the path for the flv file - use copy of move command.
A batch file to rename all .LOG files to .TXT in the 'demo' folder and all sub-folders:
CD C:\demo\
For /R %%G in (*.LOG) do REN "%%G" "%~nG.TXT"

Batch renaming files after certain character

I have a bunch of video files with names like so:
6592110904-Ivory-2.mp4
6592280588-Cornflower.mp4
6592321696-Ballet Pink.mp4
I want to rename them to get rid of everything after the first hyphen so they end up like:
6592110904.mp4
6592280588.mp4
6592321696.mp4
How do I go about doing this?
Please put the code below in a bat file, place it in directory with mp4 files. Before running real renaming, please remove "echo" before "move". please be carefull with renaming bacause (theoretically) it is possible to have same name for different files.You'll be prompted to confirm if you want to override the old one.
Code splits each filename after dash and renames the file taking first item. Good luck.
#echo off
for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.mp4"') do (
echo move "%%a-%%b" "%%a%%~xb"
)

Windows batch file - Concatenate all files in subdirectories

Need to concatenate all of the javascript files in a directory and all of its sub-directories into one file.
Right now I have a very simple command in a batch file that concatenates all of the matching files in one directory into one file:
copy C:\javascripts\*.js concatenated.js
However, this only works for the one directory and not any of its sub-directories. How can I do this same thing and also include all of the matching files in the sub-directories?
Thanks so much!
From the command line you can use
for /r "c:\javascripts" %F in (*.js) do #type "%F" >>concatenated.js
You might want want to first delete any existing concatenated.js before you run the above command.
From a batch file the percents need to be doubled
#echo off
del concatenated.js
for /r "c:\javascripts" %%F in (*.js) do type "%%F" >>concatenated.js
EDIT
It is a bit more efficient to put parentheses around the entire statement and use a single overwrite redirection instead of append redirection with each iteration. It also eliminates the need to delete the file in the beginning.
>concat.js (for /r "c:\javascripts" %F in (*.js) do #type "%F")
or from batch
#echo off
>concat.js (for /r "c:\javascripts" %%F in (*.js) do type "%%F")
I'm not aware of an approach to do that from a batch file, but you could use a tool like minify
http://code.google.com/p/minify/
to both combine JavaScript files and also minify them (e.g. by removing comments and unnecessary white space).
There are quite a few similar tools for a variety of programming environments.

Renaming files overwrites part of the filename

I ahve a directory full of files, and I want to rename each of them to have TA_ in front of the original file name. file1.txt should be renamed to TA_file1.txt. What I am getting is TA_e1.txt instead.
ren "c:*.txt" "TA_*.txt" is the command I am trying to use.
The file names are all of various lengths, and no matter what I try, it always overwrites the first 3 characters of my file name....
A simple one liner would be:
for %i IN (*.txt) DO ren "%i" "TA_%i"
This loops over all files (*.txt) and passes their name in the %i variable to the ren command. ren can then use the %i content to expand it with your desired prefix.
The command will only work for files in the current directory. For more complex things you should write a batch file. Come back if you need help with that.

Resources