We have a couple of thousand files in a directory named like this:
EXP_10000021.XM_
And need to remove the leading 1 so the new file name is:
EXP_0000021.XM_
I'm no good with batch files - any help would be appreciated!
If your filenames start all with EXP_1 it's easy.
setlocal EnableDelayedExpansion
for %%A in (EXP_1*.XM_) do (
set "filename=%%A"
set "newName=EXP_!filename:~5!"
rem ** remove the ECHO when it seems to work
ECHO ren !filename! !newName!
)
Related
I need a batch file that renames my files in according to the folder name.
For example I have this folder:
E:\PROGET\01_Progetti\1_TRATTATIVE\IT.16.9291_Fabbricato ad Milano (MI) - Ing. Bianchi\03-CALCOLO\02-XLS\
Which contains
CB-Tech_92XX - .xls
Punz_92XX - .xls
I want to rename them to
CB-Tech_9291 - .xls
Punz_9291 - .xls
Is it possible?
EDIT:
I've found this code from a guy who asked for code and didn't get any complain Rename all files in a directory with a Windows batch script
I've changed it a little bit:
#echo off
setlocal enableDelayedExpansion
for %%F in (*92XX*) do (
set "name=%%F"
ren "!name!" "!name:92XX=9XXX!"
)
#pause
Now I just have to understand how to get the path (done), extract only the numbers (not yet) and store in a variable (done).
To set a variable should be something like that
set "number=projectnumber"
SET mypath=%~dp0
ok now I've the path, need to extract only 4 characters after the IT.16.
Will edit later :)
EDIT 2:
#echo off
setlocal enableDelayedExpansion
SET mypath=%~dp0
set projectnumber=%mypath:~41,4%
for %%F in (*92XX*) do (
set "name=%%F"
ren "!name!" "!name:92XX=%projectnumber:~0%!"
)
#pause
YEAH! This works for this specific folder!!
Now I just need to understand how to search and extract the number inside the path to make it more general.
I'm looking for a function that returns the position of the first char of the string IT.16.
Any advice?
This is the complete solution of this question:
#echo off
setlocal enableDelayedExpansion
SET mypath=%~dp0
set "projectnumber=%mypath:*IT.16.=%"
set "projectnumber=%projectnumber:~,4%"
for %%F in (*92XX*) do (
set "name=%%F"
ren "!name!" "!name:92XX=%projectnumber:~0%!"
)
I have many files in many folders that I need to rename.
And example is
from cgs2016-09-05-05-40-34.xls
to cgs0905.xls
and
from cgs2016-09-06-05-40-34
to cgs0906.xls
etc
Any help would be greatly appreciated!
#Jamaz Try out the following code below on a sample of your files. Again please use it on a test sample of your files so it does not cause you issues if it makes a mistake. Thank you and please up-vote if this works for you.
setlocal enabledelayedexpansion
REM Collect list of file names in current directory that have the .xls file extension. Output to text file.
dir /b "*.xls" >xls.txt
REM For loop examines the text file for individual file names.
FOR /F "tokens=1" %%# in (.\xls.txt) do (
REM SET variable "#" to equal "token"
Set "token=%%#"
REM Extract the first 3 characters (year) from the file name and set is to variable "token"
Set "tokenchar=!token:~0,3!"
REM Extract the month characters from the file name and set the variable as "tokenmonth"
Set "tokenmonth=!token:~8,2!"
REM Extract the day characters from the file name and set the variable as "tokenday"
Set "tokenday=!token:~11,2!"
ren "%%#" "!tokenchar!!tokenmonth!!tokenday!.xls"
echo %%#
)
Pause
not the best way, but works for your examples:
#echo off
setlocal enabledelayedexpansion
for %%x in (*.xls) do (
set "filename=%%x"
ECHO ren "%%x" "!filename:~0,3!!filename:~8,2!!filename:~11,2!.xls"
)
remove the ECHO if output is ok.
Because the last nineteen characters, date and time stamp, are more likely to be constant than the first three, (especially over multiple folders), I'd change both the previous answers to cater for that rationale.
#Echo Off
SetLocal EnableDelayedExpansion
(Set _rf=C:\Users\jamaz\TestDir)
(Set _fe=xls)
If Not Exist "%_rf%\" Exit/B
For /R "%_rf%" %%I In (*.%_fe%) Do (Set "_fn=%%~nI"
Echo=Ren "%%I" "!_fn:~,-19!!_fn:~-14,2!!_fn:~-11,2!%%~xI")
Timeout -1 1>Nul
EndLocal
Exit/B
As the OP was not clear about whether the code was for multiple same level folders or subfolders rooted from a single location, I went for the latter as the previous responses had already covered that.
Change your chosen file path and extension on lines 4 and 5
If you are happy with the console output, remove echo= from line 10 and delete line 11
I have a question. Is it possibile in batch language to search in folder a part of name that is same like another file and display it.For example i got folder with files :
ggggggsss.mp3
ddddddeee.mp3
ddddddff.mp3
ssssssddd.mp3
aaaaasssss.mp3
11111ssdas.mp3
11111dddd.mp3
...
I need to display in cmd only names of files
ddddddeee
ddddddff
and
11111ssdas
11111dddddd
Because the first six letter are the same. Could someone help me with this problem?
Save this script to test.bat and run from open Cmd Prompt. Replace dir value with path to your folder with .mp3 files:
#echo off
setlocal enabledelayedexpansion
set "dir=%userprofile%\music"
set "pattern1=dddddd" & set "pattern2=11111"
pushd "%dir%"
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,6!"=="%pattern1%" echo %%G)
echo/
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,5!"=="%pattern2%" echo %%G)
popd
exit /b
See also Extract Substrings.
I don't have much experience with batch scripting but this seems like a suitable task for it:
I have a very large directory with recordings of a specific extension, say '.wav'. In the same folder, I'm supposed to have, for each of these recordings, an xml file, named the exact same as the recording filename, except with the extension .xml instead of .wav.
Now, I notice that the '.xml' files total count is slightly less then the '.wav' total; i.e some of the xml files are missing.
How can I extract a list of all recordings with missing xml files?
#echo off
for %%F in (*.wav) do if not exist "%%~dpnF.xml" echo %%F
You don't really need a batch file. You could enter the following directly on the command line:
for %F in (*.wav) do #if not exist "%~dpnF.xml" echo %F
Actually, you don't need the ~dp modifiers as I have written the code. But if you include path information like in (somepath\*.wav), then they become important.
This a little approch can did the trick
You should just change Set folder=c:\temp to your folder that contains your files :
#echo off
set folder=c:\temp
set ext1=wav
set ext2=xml
Set Log=c:\MissingFiles.txt
set /a count=0
cd /d "%folder%"
setlocal enabledelayedexpansion
(
for %%a in (*.%ext1%) do (
for %%b in (*.%ext2%) do (
If /I not "%%~na"=="%%~nb" ( SET /A COUNT+=1 && echo "%%~dpnxb" )
)
)
echo.
ECHO The total number of missing files is !COUNT!
Endlocal
) > %Log%
Start "" %Log%
At work we use a bespoke program to search through a directory tree and find an individual image file. These files are stored in folders that have a 7-digit name, starting with '18' - so for instance '1873456', '1873457', '1873458' etc. The problem I have is at some point last year the procedure that creates these folders and populates the images in them reached '1899999' - and then rolled over to '18100000' and carried on like that for over 4,000 folders before it was caught and corrected.
The bespoke program we use can only handle seven-digit folder names. What I would like to do is create a batch file that renames all the eight-digit folders by removing the extra '1' in the name, so '18100000' becomes '1800000' and so forth until '18104013' becomes '1804013'.
Can anyone help?
Run this in the base folder, it will not change any folders.
A file called renfile.bat.txt will be created that contains the rename command for the folders that match the filespec. Examine it in notepad to see if it is ok and then rename it to renfile.bat and run it.
It's not tested.
#echo off
setlocal enabledelayedexpansion
for /d /r %%a in (18??????) do (
set "name=%%~nxa"
>>renfile.bat.txt echo ren "%%a" "!name:~0,2!!name:~3!"
)
Something like
for /l %%x in (100000,1,104013) do (
set oldsuffix=%%x
set newsuffix=%oldsuffix:~-5%
ren 18%%x 18%newsuffix%
)
setlocal enableextensions enabledelayedexpansion
for /d /r "c:\somewhere" %%f in (181?????) do (
set "name=0" & set /a "name+=%%~nf" 2>nul
if !name! gtr 1899999 ren "%%~ff" "18!name:~-5!"
)