I have a file called xxxxxxx_12345.pdf and I try to delete the suffix and the result would be xxxxxxx.pdf.
I tried following:
forfiles /S /M *_12545.pdf /C "cmd /c rename #file #fname*.pdf"
but it could not change the file name.
Can somone help to solve this issue?
thanks
I'm sure there is a way to do the job with forfiles command, however, it would be much easier to use regular for loop for this.
#echo off
pushd %~dp0
setLocal EnableDelayedExpansion
for %%f in (*_12545.pdf) do (
set "CurrentFileName=%%~nf"
set "RenameTo=!CurrentFileName:~0,-6!"
echo.ren !CurrentFileName!%%~xf !RenameTo!%%~xf
)
pause>nul
By setLocal EnableDelayedExpansion, user can use delayed environment variables, which will enable user to use something like !variable! instead of %variable%.
set "CurrentFileName=%%~nf"
%%~nf will give you the name of the file without extension, so in this case, CurrentFileName will be FileName_12545. Type for /? to see the full explanation of the syntax.
set "RenameTo=!CurrentFileName:~0,-6!"
Notice that I used !variable:~0,-6! to remove suffix. This is String Manipulation. The user is storing a string without suffix that has a length of 6 (which is _12545 in this case).
echo.ren !CurrentFileName!%%~xf !RenameTo!%%~xf
I placed echo in front of ren so you can check before you actually rename it. %%~xf will return the file extension including .(dot) in front of it. In this case, the file extension is .pdf.
I hope this help a bit.
Related
I have several folders named:
001TRIAL1
002TRIAL2
003TRIAL3
...etc.
I'm trying to rename all folders using a batch-file and truncate them in order to get:
TRIAL1
TRIAL2
TRIAL3
...etc.
I have tried this code:
for /D %%X in (C:\FOLDER1\FOLDER2\*) do (
move %%X %%X:~3,10%
)
Pause
But I'm unable to get the result I want.
This script does not work and I don't know how I can do that. The rename allows to rename folders but I'm not able to truncate the folders that I want.
You cannot expand a metavariable like that, you need to set the value to a proper variable, then perform the expansion. Additionally, as the variable would be set within a code block, you'd need to enable delayed expansion. Finally, you'd use Rename not Move, which would require that the directory already existed, and you'd need to delete the original directory post rename.
Try this:
#Echo Off
SetLocal DisableDelayedExpansion
For /D %%A In ("C:\FOLDER1\FOLDER2\*") Do (
Set "_d=%%~nxA"
SetLocal EnableDelayedExpansion
If Not Exist "%%~dpA!_d:~3!" Ren "%%A" "!_d:~3!"
EndLocal
)
I have seen previous issues here on it anyway, but I'm asking for the particular case at hand.
Currently trying to rename some files, but I actually want the file with the duplicate name to be overwritten.
#echo off
setlocal
for /R %%A in ("tgt*.nif") do (
set "fname=%%~A"
call ren "%%fname%%" "%%fname:*tgt=%%"
)
pause
Any suggestions?
EDIT: As suggested, an example. Well this is not real, but simply putting it (Reading this it's not "simply" but pft):
I have two files, one which is TimeGoog.pdf and another which is Goog.pdf. Now I have these variations of these two files in many folders, so it may be TimeToot.pdf and Toot.pdf. I want to rename all the TimeGoog.pdf to Goog.pdf, deleting the old Goog.pdf at the same time as it's been overwritten.
The script above does that, except because it's renaming to something that's already there, it won't do it. Note that the batch code I put above would work in the parent folder, and all the sub folders would take effect.
#echo off
setlocal enableextensions disabledelayedexpansion
for /r %%A in ("tgt*.nif") do (
set "folder=%%~dpA"
set "fileName=%%~nxA"
setlocal enabledelayedexpansion
echo move /y "!folder!!fileName!" "!folder!!fileName:*tgt=!"
endlocal
)
pause
This helped me : for renaming files with no extension, to files with a extension. (.csv)
ren *.* *.csv (wrong)
gives : A duplicate file name exists, or the file cannot be found
ren *.* *.*csv (works)
Works.
Having troubles with cmd syntax, trying to delete files with specific extension in a specific folder that don't have sidecars. eg.: if folder contains:
1.A, 1.B, 2.A, 3.A, 4.A, 4.B
bat should only delete
2.A, 3.A
..hope that makes sense.
The code I've got so far must be real close, unfortunatelly not working
#echo off
FOR %%x IN (%1\*.A) DO
(
IF not exist "%1\%x.B" del "%1\%x.A"
)
Any help mostly appreciated.
Comments to question are correct
FOR variable percents must be consistently doubled when used in batch script
Open paren must be on same line as DO. But there isn't any need for parens with such a simple script.
Also, you want only the base name of the FOR variable, so you need the ~n modifier.
I made the code a bit more robust by using PUSHD at beginning.
#echo off
pushd %1
for %%F in (*.A) do if not exist "%%~nF.B" del "%%F"
popd
I am trying to rename dbf files in a folder. The batch script below is currently set up to rename the file to its current name. How do I modify the syntax to rename the files with just the first 8 characters, including the .dbf extension? I’ve tried using “%%~nx:~8.dbf” for the destination name, but it doesn’t seem to work. Thank you!
for %%x in ("C:\Users\user\Documents\monthly_adhoc\importclm\*.dbf") do (
rename "%%x" "%%~nx.dbf")
The input files would be something like this:
12345678_XXXXXXX_KKKKKK.dbf
12364178_XXXXXXX_KKKKKK.dbf
12124668_XXXXXXX_KKKKKK.dbf
12342178_XXXXXXX_KKKKKK.dbf
I’d want the output files to be this.
12345678.dbf
12364178.dbf
12124668.dbf
12342178.dbf
This should do it.
#echo off
setlocal EnableDelayedExpansion
for %%x in ("C:\Users\user\Documents\monthly_adhoc\importclm\*.dbf") do (
set newname=%%~nx
ren "%%x" "!newname:~0,8!.dbf"
)
You need an interim variable (FileName) for extracting the first 8 characters of each filename:
setlocal EnableDelayedExpansion
for %%X in ("C:\Users\user\Documents\monthly_adhoc\importclm\*.dbf") do (
set FileName=%%~nX
rename "%%~X" "!FileName:~0,8!%%~xX")
endlocal
The setlocal/endlocal block enables delayed expansion.
Take a look at this post for an explanation.
Trying to use the directory outputed by this line in my batch code.
findstr /M search_text "C:\Users\user\Desktop\*"
This output's a file directory.
"C:\Users\whatever\blabla"
I know I can just add >> and print it to a text file but I'd rather not go that route as I'm not sure how to pull the dir back into the command line in the first place. Is there somehow I can do like a Set=%k command to a variable for later use. Sorry I'm still very new to this. Thanks for any help!
You can do this
for /f %%i in ('findstr /m "search_text" "C:\Users\user\Desktop\*"') do set file=%%i
Then you have the output in the %file% variable.
Hope this helps.