A duplicate file name exists, or the file cannot be found - batch-file

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.

Related

Truncate and rename subdirectories within a directory

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
)

CMD: Script to batch rename files in a folder based upon the first 8 characters of its name

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.

How to rename all folders starting with 'zz_' from 'zz_name' to 'name'?

I'm trying to write a batch to rename multiple folders. I have got the below script which I'm pretty sure will work if I target only the desired folders in the directory. I want to remove characters from a folder name so zz_name can be renamed to name.
The problem is my script is looking on the first 3 characters and rename all folders, not just those with zz_. I'm thinking at worst I could move the zz_ folders to their own directory, rename them, and move them back. But that seems like a long way to go.
Is it possible to target only zz_ in folder name?
setlocal
for /d %%i in (*) do call :rename %%i
goto :eof
:rename
set ZZDIR=%1
ren "%ZZDIR%" "%ZZDIR:~3%"
Adam Smith has answered already the question.
However, here is an improved batch code for this question.
#echo off
setlocal
for /d %%i in (zz_*) do call :RenDir %%i
endlocal
goto :EOF
:RenDir
set "ZZ_DIR=%1"
set "NEWDIR=%ZZ_DIR:~3%"
if not "%NEWDIR%"=="" if not exist "%NEWDIR%" ren "%ZZ_DIR%" "%NEWDIR%"
This batch file ignores a directory with just zz_ as name.
And it also does not try to rename for example zz_xxx to xxx if there is already a directory xxx.

batch renaming multiple file names

I'd like to rename specific image names from *_PolishedChrome.jpg to be *-pch.jpg. I tried a bunch of different ways, included the rename DOS command, but cannot find the right way to do it. The files are in a folder on my desktop and they are all .jpg images.
There are multiple files with the last part of the image name.
Example: rename 3-04012C_PolishedChrome.jpg to be 3-04012C-pch.jpg
I'd appreciate any help, thanks!
I think this will do what you are looking for (just place it in the same folder as the jpgs and run):
Remember to make a backup of your files before running this, as it does move files on the disk. It could be done with rename, but that is more effort, as I'd have to parse out the file name.
Also note that running this twice will re-rename your files, so it can only really be run once within a folder... that can be changed, but this is more of a starting point.
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /r %%i in (*.jpg) do (
set str=%%i
set str=!str:_PolishedChrome.jpg=!-pch.jpg!!
move %%i !str!
)
DEBUG version - prints out a log.txt to see what went wrong.
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /r %%i in (*.jpg) do (
set str=%%i
set str=!str:_PolishedChrome.jpg=!-pch.jpg!!
move %%i !str!
#echo move %%i !str! >> log.txt
)

adding dates to copied files

I am trying to add the dates to copied files from one directory to another directory. This is how it should look like
Original file name: XEsalary.csv
Result file name: XEsalary-2013-02-15.csv
Here is my code:
set REMOTE=U:\
set LOG=C:\Integration\FE\log_test
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4%
set LOCAL=C:\FTP\VC\test
cd %LOCAL%
xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log
xcopy /y /f /v "%LOCAL%\*.csv" "%LOCAL%\archive\*.csv"
:: assist with turning this into a for loop
ren %LOCAL%\archive\*.csv %LOCAL%\archive\*%PVM%.csv
echo. >>%LOG%\%PVM%.log
copying works just file. It is the renaming part which is not working. Any help please?
Thx in advance :-)
Using wildcards with RENAME is tricky. There is no good official documentation, but I have experimented and published rules as to how it works: See How does the Windows RENAME command interpret wildcards?
You can accomplish your rename with the following command, as long as none of your file names include dots (the last extension dot is OK).
ren "%LOCAL%\archive\*.csv" ????????????????????-%PVM%*
The number of ? must be greater than or equal to the longest name in the folder.
The result will be incorrect if a file name contains a dot. For example, part1.part2.csv would become part1-2013-02-15.part2.csv.
Another option is to use a FOR loop. You can safely append the date to any file using this technique.
for %%F in ("%LOCAL%\archive\*.csv") do ren "%%F" "%%~nF-%PVM%%%~xF"
BUT, both solutions have a problem if you rerun the process on a later date. New files will be added to the archive, but both new and old archive files will be renamed to the current date. That won't work.
Better to copy and rename the file in one step using a FOR loop as suggested in rojo's answer.
Or better yet, create a new archive folder with the date in the folder name, and then copy the files to the dated archive folder without renaming :-)
set REMOTE=U:\
set LOG=C:\Integration\FE\log_test
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4%
set LOCAL=C:\FTP\VC\test
cd %LOCAL%
xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log
md "%LOCAL%\archive\%PMV%
xcopy /y /f /v "%LOCAL%\*.csv" "%LOCAL%\archive\%PVM%"
You might want to include the time in the folder name if the process could be run multiple times in the same day.
Windows doesn't let you rename with a wildcard. You have to name each file in a for loop. Might as well do the renaming as you're copying. Does this do what you intended?
#echo off
setlocal
set REMOTE=U:\
set LOG=C:\Integration\FE\log_test
set PVM=%DATE:~9,4%-%DATE:~6,2%-%DATE:~3,2%
set YY=%DATE:~9,4%
set LOCAL=C:\FTP\VC\test
xcopy /y /f /v "%LOCAL%\*.csv" "%REMOTE%\" >>%LOG%\%PVM%.log
pushd "%LOCAL%"
for %%I in (*.csv) do (
rem Uncomment the following line to log the copy to archive.
rem echo copy "%%I" -^> "%LOCAL%\archive\%%~nI-%PVM%.csv" >>%LOG%\%PVM%.log
copy "%%I" "%LOCAL%\archive\%%~nI-%PVM%.csv">NUL
)
echo. >>%LOG%\%PVM%.log
popd

Resources