Want to rename a file through windows batch file scripting - windows

Here is my file MY_APP_CON_123_yyyymmdd.hhmmss.txt where "yyyymmdd.hhmmss" is variable data. I want to rename it to MY_APP_CON_0123_yyyymmdd.hhmmss.txt (Zero infront of 123). When I use ren command as shown below, the first y disappears from the file name.
REN MY_APP_CON_123_* MY_APP_CON_0123_*
Any solutions ?

Not answer, but explaining why your rename isn't working:
cmd.exe/command.com's wildcard renaming is position based, e.g.
filename: abc123.txt
command: ren abc*.txt abd1*.txt
will do:
abc123.txt - original name
||||**.txt
abd1**.txt - rename pattern
----------
abcd23.txt - new filename
https://superuser.com/questions/475874/how-does-the-windows-rename-command-interpret-wildcards
Wildcard renaming where you want to change parts of the filename and the new part has a different length from the original part gets very hairy.

Here is a pure batch solution.
#echo off
for %%F in (MY_APP_CON_123_*) do #set "name=%%F"&call ren "%%name%%" "%%name:*MY_APP_CON_=MY_APP_CON_0%%"
Or, with delayed expansion:
#echo off
setlocal enableDelayedExpansion
for %%F in (MY_APP_CON_123_*) do #set "name=%%F"&ren "!name!" "!name:*MY_APP_CON_=MY_APP_CON_0!"
But I would use my JREN.BAT regular expression renaming utility - a hybrid JScript/batch script that runs natively on any Windows machine from XP onward.
call jren "^MY_APP_CON_123_" "MY_APP_CON_0123_" /i /fm *.txt

for %%a in (MY_APP_CON_123_**) do set "name=%%a" & ren %name% %name:123=0123%
This worked for me

Related

Batch file to copy/rename directory recursively and find/replace file contents

A windows batch file is needed to make a copy of a directory tree and rename occurrences of a given old_name to a given new_name, please see example below.
I looked at robocopy and xcopy for no avail
String old = "old_name";
String new = "new_name";
current directory:
C:\old_name
C:\old_name\table
C:\old_name\garage\old_name\chair\a.file (contains text I am OLD_NAME)
C:\old_name\garage\old_name\b.file (contains text I am old_name)
desired outcome:
C:\new_name
C:\new_name\table
C:\new_name\garage\new_name\chair\a.file (contains text I am NEW_NAME)
C:\new_name\garage\new_name\b.file (contains text I am new_name)
edit:
showing one node in the path which is not being changed to the new name.
old_name=twintyone
new_name=one
C:\Users\fredJ\AndroidStudioProjects\one\app\src\main\java\mx\com\businessman\twintyone
I'm not aware of any single utility that will do everything you want, but if you deploy my JREPL.BAT and JREN.BAT utilities, then a simple batch script can easily and efficiently accomplish your goal.
Both JREPL.BAT and JREN.BAT are hybrid JScript/batch scripts that run natively on any Windows machine from XP onward - no executables need to be copied or installed.
The batch script has 3 simple steps:
Use XCOPY to copy your directory tree
Use JREN.BAT to appropriately rename OLD_NAME folders to NEW_NAME
Use FINDSTR to identify files that contain OLD_NAME, iterate the result with FOR /F, and edit each found file with JREPL.BAT
I also add a few ECHO statements so you can follow the progress
Note - the following is untested. If there is a bug, there should be a simple fix - the basic design is sound
#echo off
set "old=OLD_NAME"
set "new=NEW_NAME"
xcopy "c:\%old%\*" "c:\%new%" /i /s
call jren "^.+" "%new%" /d /s /p "c:\%new%" /fm "%old%"
for /f "delims=" %%F in ('findstr /mspc:"%old%" "c:\%new%\*"') do (
echo Editing "%%F"
call jrepl "%old%" "%new" /l /f "%%F" /o -
)
echo(
echo Done!

Feed in a list of find and replace values with batch

I'm looking for a way to find and replace multiple words in a text file using a Windows batch script.
I know replacing a word can be done so with this bat script:
#echo off &setlocal
set "search=%1"
set "replace=%2"
set "textfile=Input.txt"
set "newfile=Output.txt"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
del %textfile%
rename %newfile% %textfile%
Now, I just have to take it one step further and feed in the search and replace strings from another file similar to this format:
search_string1, replace_string1
search_string2, replace_string2
search_string3, replace_string3
.
.
.
I would think that I would somehow process the file line by line, and parse them into two variables (search, replace) and then feed that into the script above. Any thoughts? I'm new to Windows batch scripts and have never really made one before so mind my newbie questions.
This type of text replacements are slow and prone to fail when performed via a Batch file. I wrote FindRepl.bat program that is a Batch-JScript hybrid script that not only run much faster and with no errors, but it also allows to perform the multiple replacements you are looking for in just one processing pass of the data file. JScript is a programming language that is included in all Windows versions from XP on. Using FindRepl.bat program you may solve your problem this way:
#echo off
setlocal EnableDelayedExpansion
set "search="
set "replace="
for /F "tokens=1,2 delims=," %%a in (replacements.txt) do (
set "search=!search!|%%a"
set "replace=!replace!|%%b"
)
set "search=!search:~1!"
set "replace=!replace:~1!"
< Input.txt FindRepl =search /A =replace > Output.txt
Note that all text placed after the comma in the replacements file is the replacement string, including spaces.
You may download FindRepl.bat program from this site. Place it in the same folder of previous program or, better yet, in a folder included in %PATH%, so you may use it directly.

Renaming issue within command prompt

i'm trying to rename a file called Pawn.Stars.S01E02.Hello
i'm using
cmd ren "Pawn*Stars*" "Pawn Star$.*"
which does rename the file but renames it to Pawn Star$.Stars.S01E02.Hello
i have also tried
ren Pawn.Stars*.txt PawnStar$*.txt
which gives the same result.
The Name i'm trying to get is Pawn Star$.S01E02.Hello
Where am i going wrong or what needs to change?
You haven't stated what name you are trying to achieve, so I cannot give you a command that works.
But I can tell you that it is not possible to remove or change the dot between Pawn and Star$ using a simple REN command with wildcards. See How does the Windows RENAME command interpret wildcards? for an explanation.
One option would be to write a batch script to do the rename. There are also 3rd party utilities that provide more sophisticated file renaming capabilities.
Update based on comment from OP:
The following will do the rename from the command line:
for /f "delims=" %A in ('dir /b /a-d pawn.stars.*') do #for /f "delims=. tokens=2*" %B in ("%A") do #ren "%A" "Pawn Star$.%C"
Double up the percents if you use the command in a batch script.

Rename multiple folders with pattern in name using Batch

I have 100 folders under a directory with naming in a pattern.
Ex: DeDeP001M1TSub, DeDeP002M1TSub,...,DeDeP100M1TSub.
I am looking for a command line option to rename all the folders. I just want to change M1 to M2 as in DeDeP001M1TSub becomes DeDeP001M2TSub. I know of 3rd party applications that can do this, but am looking for a command line option to use in a bat file along with bunch of other stuff. Please help(Fairly new to the area)!!
So far have tried ren,mv but to no effect(not very familiar with dos scripting).
This should do it:
#echo off
setlocal enabledelayedexpansion
for /d %%a in (*) do (
set "p=%%a"
set "fp=!p:~0,8!" & set "tp=!p:~10!"
echo ren %%a !fp!M2!tp!
)
Remove the echo once you verify the output is what you want to do the actual rename.

For loop in batch file reading a file of File Paths

I want to write a Windows batch file script that will loop through a text file of FILE PATHS, do some work using data from each file path, then ultimately delete the file.
I started by running the FORFILES command and sending its output (the #PATH parameter is the full path of any file it matches) to a text file (results.txt).
I end up with a results.txt file like this:
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir2/fileC.log"
"C:/Windows/Dir3/fileB.log"
What I want to do is:
Use a FOR loop and read each line in the results.txt file
For each line (file path), strip out the directory name that the log file is sitting in (ie: Dir1, Dir2, etc..) and create a directory with that SAME name in a different location (ie. D:/Archive/Backups/Dir1, D:/Archive/Backups/Dir2, etc..) -- assuming the directory doesn't exist.
Move the actual .log file to a zip file in that directory [I have code to do this].
Delete the .log file from its original location. [Pretty straightforward]
I'm having trouble figuring out the best way to accomplish the first 2 steps. My FOR loop seems to stop after reading the very first line:
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10 delims=\" %%G in ("results.txt") DO (
...
)
You don't want to parse the path with the tokens/delims options because you don't know how many directory levels you are dealing with. You want to preserve each line in its entirety. TO parse the path you want to use the FOR variable modifiers. (type HELP FOR from the command line and look at the last section of the output)
%%~pG gives the path (without the drive or file name). If we then strip off the last \, we can go through another FOR iteration and get the name (and possible extension) of the file's directory by using %%~nxA.
The toggling of delayed expansion is just to protect against a possible ! in the path. If you know that no path contains ! then you can simply enable delayed expansion at the top of the script and be done with it.
EDIT - this code has been modified significantly since Aacini pointed out that I misread the requirements. It should satisfy the requirements now.
for /f "usebackq delims=" %%G in ("results.txt") do (
set "myPath=%~pG"
setlocal enableDelayedExpansion
for /f "eol=: delims=" %%A in ("!myPath:~0,-1!") do (
endlocal
if not exist d:\Archive\Backups\%%~nxA md d:\Archive\Backups\%%~nxA
rem ::zip %%G into zip file in the D: location
rem ::you should be able to create the zip with the move option
rem ::so you don't have to del the file
)
)
I wrote this to timestamp files before offloading to SFTP.
Hope you find it useful.
The timestamp coding may seem irrelevant to your issue, but I left it because it's a good example of dissecting the filename itself.
I suggest you put an ECHO in front of the REN command for testing. Different shells may have different results.
In the end, the delayedexpansion command wasn't necessary. It was the sub-routine that fixed my issues with variables inside the loop. That could possibly be because of my OS ver. (Win 8.1) - It wouldn't hurt to leave it.
#echo off
cls
setlocal enabledelayedexpansion
if %time:~0,2% geq 10 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
if %time:~0,2% leq 9 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
echo TimeStamp=%TIMESTAMP%
echo.
for %%G in (*.txt) do (
set OLDNAME=%%G
call :MXYZPTLK
)
dir *.txt
goto :EOF
:MXYZPTLK
echo OldName=%OLDNAME%
ren %OLDNAME% %OLDNAME:~0,-4%_%TIMESTAMP%%OLDNAME:~-4,4%
echo.
:END
You have two minor problems:
The path separator in the file is '/' but you use '\' in the for loop.
The quotes around "results.txt" stop it working.
This works. Don't write quotes to results.txt and you won't get a quote at the end of the filename.
#echo off
FOR /F "tokens=3,4 delims=/" %%I in (results.txt) DO (
REM Directory
echo %%I
REM File
echo %%J
)

Resources