I have several folders withing the same directory that are named like that :
001_Trial1
002_Trial2
003_Trial3
Trial4
Trial5
004_Trial6
005_Trial7
etc ...
I want to rename the folders in order to get
Trial1
Trial2
Trial3
Trial4
Trial5
Trial6
etc...
I tried to truncate it but the problem is that it will also delete the four first characters when the folder is "Trial3" it will rename it "3".
SetLocal DisableDelayedExpansion
For /D %%A In ("%1\*") Do (
Set "_d=%%~nxA"
SetLocal EnableDelayedExpansion
If Not Exist "%%~dpA!_d:~4!" Ren "%%A" "!_d:~4!"
EndLocal
)
I want all the outputs to start with "Trial"
The first thing I noticed looking at your code is, that you need to replace %1 with %~1 to get rid of the quotes.
I would then use a for /f to cut off the first part of your folder-names, so you don't need delayedExpansion and the code can handle folder-names with more or less digits at the beginning (and of course does not rename folders that already have the wanted name-format).
for /D %%D in ("%~1\*") do (
for /F "tokens=2 delims=_" %%N in ("%%~nD") do (
if not exist "%%~dpD%%~N%%~xD" ren "%%~fD" "%%~N%%~xD"
)
)
Related
I want to loop through a folder and let run an algorithm on each .tif file found. Unfortunately this does not work for files which have a space character in their name. As my path already contains folders with space, i put the variable which stores the path name in double-quotation marks.
FOR /F %%k IN ('DIR /B "%mypath_import%"*.tif') DO (
SET infile=%%k
SET outfile=!infile:.tif=_UTM.tif!
REM ... do something
This is my attempt so far but it won't work for the files which include a space as well.
You done need all that. You can use the normal for loop without having to use /f
#echo off
setlocal enabledelayedexpansion
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do (
set "infile=%%~i"
echo "!infile:.tif=UTM.tif!"
)
The above will however echo full path to and file name, if you want filename only with extension:
#echo off
setlocal enabledelayedexpansion
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do (
set "infile=%%~nxi"
echo "!infile:.tif=UTM.tif!"
)
or without the need to delayedexpansion
#echo off
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do echo %%~dpni_UTM%%~xi
and again if you require the name and extension only.
#echo off
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do echo %%~ni_UTM%%~xi
EDIT
As per comment from #Stephan, keep in mind if you are doing actual renames and you run the script more than once it will keep on appending _UTM each time. So you'll get filename_UTM_UTM.tif etc. So you can exclude files from the loop by including findstr
for /f "delims=" %%i in ('dir /b *.tif ^|findstr /eiv "_UTM.tif"') do echo %%~ni_UTM%%~xi
My script iterates through a list of user profile paths and copies a bunch of files into them. This works so far now I need to edit the copied .xml files so I thought I'll just add another for /r for these folders and do my thing. But I can't get this script working and I really don't know why. It's only entering the for /r loop when I substitute !PATH! with a constant value.
SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%a in (users.txt) do (
SET PATH1=%%a\appdata\local\lazarus
for /R "!PATH1!" %%x in (*.xml) do (
call :fart "DKIUSER" "%%a" "%%x"
)
)
thats the content of my users.txt:
C:\Users\DKIUSER
C:\Users\dkiuser.DCJAN
C:\Users\ladmin
I have about 1000 images and they have name like "IMG-12223". I want to rename them to 1 2 3 4 ... 1000. How can I do that. I have written a batch script which list the files but I don't know how to rename each file. e.g. rename first image with name "IMG-12223" to 1 , second image with name "IMG-23441" to 2 and so on ...
for /r %%i in (*) do (
echo %c%
)
Here's the script. Just put the script in your folder and run it.
#echo off & setlocal EnableDelayedExpansion
set a=1
for /f "delims=" %%i in ('dir /b *') do (
if not "%%~nxi"=="%~nx0" (
ren "%%i" "!a!"
set /a a+=1
)
)
If you want to keep the extensions, i.e. rename "IMG-12223.jpg", "IMG-12224.jpg", etc to "1.jpg", "2.jpg", etc, you may use the following script.
#echo off & setlocal EnableDelayedExpansion
set a=1
for /f "delims=" %%i in ('dir /b *.jpg') do (
ren "%%i" "!a!.jpg"
set /a a+=1
)
[Update] Here're explanations for the lines mentioned in Jack's comment.
setlocal EnableDelayedExpansion
In general, we want the variable a to be delayed expansion when it's executed but not the line is read. Without it, the variable a cannot get its increased value but always 1.
For the detail of EnableDelayedExpansion, please refer to the answer https://stackoverflow.com/a/18464353/2749114.
for /f "delims=" %%i in ('dir /b *.jpg')
Here dir with /b option, lists only file names of all jpg files.
The for loop traverses and renames all jpg files.
For the delims option, since the default delimiter character is a space, without the option delims=, it fails with the image files with spaces in the file names. I.E. for an image file named "img with spaces.jpg", without the option, the value of %%i is "img" but not the whole name "img with spaces.jpg", which is incorrect.
For for loop, please refer to the page http://ss64.com/nt/for_f.html.
if not "%%~ni"=="%~n0"
I have change it to if not "%%~nxi"=="%~nx0" to be more accurate. And the codes attached have been updated.
It's actually used to avoid to rename the bat file itself. If we limit the renaming only upon "jpg" files, then the line is not needed.
%%~nxi is the file name with extension for each file traversed. And %~nx0 is the running bat file with extension. For details, please refer to the page DOS BAT file equivalent to Unix basename command?.
There is no need for a batch script. A simple one liner from the command line can do the job :-)
I use DIR /B to generate the list of files, piped to FINDSTR to number the files, all enclosed withn FOR /F to parse the result and perform the rename.
for /f "delims=: tokens=1*" %A in ('dir /b *.jpg^|findstr /n "^"') do #ren "%B" "%A%~xB"
Double the percents if you want to put the command in a batch script.
Try this, you have pair of namevalues in a text file then loop values and do the magic. Namevalues are separated by empty spaces. This allows you to map old->new filenames accordingly. Or you keep idx+1 counter and use it for new filenames.
keyvalue.bat
#echo off
set idx=0
for /F "tokens=1,2" %%A in (keyvalue.txt) do call :PROCESS "%%A" "%%B"
GOTO :END
:PROCESS
set var1=%~1
set var2=%~2
set /A idx=%idx%+1
echo %var1% goes to %var2% (%idx%)
GOTO :EOF
:END
pause
keyvalue.txt
file888.dat newfile1.dat
file333.dat newfile2.dat
file9.dat newfile3.dat
file01.dat newfile4.dat
I am trying to loop through every folder under "source" and remove the version extension that I've added.
For example,
\pathTo\source\FirstComponent.1.5\...
\pathTo\source\SecondComponent.4.6\...
Changed to,
\pathTo\source\FirstComponent\...
Here's what I have so far...
SETLOCAL
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%") DO FOR /f "delims=." %%j IN ("%%i") DO REN "%%~i" "%%~j%%~xi"
foxidrive gave already the right help to find the solution.
#echo off
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO FOR /f "delims=." %%j IN ("%%i") DO REN "%%~i" "%%~nj"
set SourceDir=
%%~nj instead of %%j is necessary to remove the path to the folder and get only new name of folder without path.
And also possible is following as suggested by Andriy M:
#echo off
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO FOR /f "delims=." %%j IN ("%%~ni") DO REN "%%~i" "%%j"
set SourceDir=
That is even better in case of one of the parent folders contains also a dot in name.
But if all folders have a dot in name like Component.One before the version string which must be kept, the following batch code can be used:
#echo off
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO FOR /f "tokens=1,2* delims=." %%j IN ("%%~ni") DO REN "%%~i" "%%j.%%k"
set SourceDir=
And one more solution which removes the last 4 characters from folder name if last but third character is a dot:
#echo off
setlocal EnableDelayedExpansion
set "SourceDir=C:\pathTo\source"
FOR /d %%i IN ("%SourceDir%\*") DO (
set "FolderName=%%~nxi"
if "!FolderName:~-4,1!"=="." REN "%%~i" "!FolderName:~0,-4!"
)
endlocal
If the folder names that you need to fix are always in this format
name.number.number
and you need to remove the .number.number part, you can also use this method:
SETLOCAL
SET "SourceDir=C:\pathTo\source"
FOR /D %%I IN ("%SourceDir%") DO (
FOR %%J IN ("%%~nI") DO RENAME "%%~I" "%%~nJ"
)
Basically, the idea is to treat the version numbers as name extensions. The inner loop uses the %%~nI expression to take just the folder name without the extension, i.e. without the last .number, and the result is assigned to %%J. In a similar fashion, the RENAME command uses %%~nJ to get rid of the remaining .number (the first one in the original name). And so, using the FirstComponent.1.5 name as an example, the RENAME command essentially ends up like this:
RENAME "...\FirstComponent.1.5" "FirstComponent"
Since the method essentially just drops the last two extensions, you can have however big version numbers: 10.123, 2014.12... The only limitation to the method is that version numbers must always consist of exactly two parts. (It could additionally support single-part version numbers if your names were not allowed to have a ..)
I am trying to rename files after the user inputs a string they want to remove from the file name. This works fine except when I want to rename files that are in a different location than the script:
Here is what i have so far which works if I dont specific the file path (e.g. remove C:\DATABASE\*.* /s)
SET /P X=Type in the String that you want to remove and then press ENTER:
set deletestring=%X%
for /f "delims==" %%F in ('dir C:\DATABASE\*.* /s /b ^| find "%deletestring%"') do (
set oldfilename=%%F
set newfilename=!oldfilename:%deletestring%=!
Ren "!oldfilename!" "!newfilename!"
)
Thanks!
Use this instead. e.g.:
remove *.*
or
remove "relative path\*.*"
or
remove C:\DATABASE\*.*
or
remove "C:\My Database\2010-*.bak"
Meaning that a directory and file mask must be specified. Here's the remove.bat file:
#echo off
setlocal enabledelayedexpansion
set mask=%~1
set mask=!mask:%~dp1=!
if not exist "%~1" (
echo No files found
goto :eof
)
pushd "%~dp1"
SET /P X=Type in the String that you want to remove and then press ENTER:
set deletestring=%X%
for /f "delims==" %%F in ('dir "%mask%" /s /b ^| find "%deletestring%"') do (
set oldfilename=%%F
set newfilename=!oldfilename:%deletestring%=!
Ren "!oldfilename!" "!newfilename!"
)
Your primary problem you are running into is that the 1st argument to REN can accept full path info, but the 2nd can only contain the new name without path info. You can use the ~nx modifier to extract the name and extension from the full path reported by the FOR /F command.
Your FOR /F options are not reliable - it will break if the file name contains =. You want to set delims to nothing instead.
This problem is actually more complicated than it first looks. Your code will attempt to rename both files and directories. If you want to rename the directories then you must rename in reverse alpha order because the entire list is built before any thing is renamed. If you process in normal alpha order and rename a directory, then subsequent entries within that directory will not be found.
The FIND filter in the IN() clause is not necessary. Ideally your filter should only match the file or directory name, not the path. That is doable, but a bit tricky. I would simply skip the filtering in the IN() clause and do it in the DO clause.
A file or directory name can contain ! character. But the FOR variable expansion will be corrupted if it contains ! and delayed expansion is enabled. The problem can be avoided by toggling delayed expansion on and off within the loop.
It is possible for the entire name to be removed by the search and replace, but you cannot rename a file to nothing. So I added a test to ensure there is a name left.
setlocal disableDelayedExpansion
SET /P "X=Type in the String that you want to remove and then press ENTER:"
for /f "delims=" %%F in ('dir C:\DATABASE\* /s /b ^| sort /r') do (
set "old=%%F"
set "file=%%~nxF"
setlocal enableDelayedExpansion
set "new=!file:%X%=!"
if defined new if !new! neq !file! ren "!old!" "!new!"
endlocal
)
If you don't really want to rename directories then you need to add the /A-D option. I first thought you could use a FOR /R statement, but that could potentially cause the same file to be renamed twice. FOR /F buffers the entire result set before processing any files, but FOR /R does not.
setlocal disableDelayedExpansion
SET /P "X=Type in the String that you want to remove and then press ENTER:"
for /f "delims=" %%F in ('dir C:\DATABASE\* /s /b /a-d') do (
set "old=%%F"
set "file=%%~nxF"
setlocal enableDelayedExpansion
set "new=!file:%X%=!"
if defined new if !new! neq !file! ren "!old!" "!new!"
endlocal
)