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%!"
)
Related
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'm trying to write a Windows batch file to rename a set of files based on their original name. What I essentially want to do is find text within a file name and replace it with other text.
For example, if the files had the naming structure "Family Christmas 001.jpg" I might want to change it to "Photos - Xmas 001.jpg". ie replace "Family Christmas" with "Photos - Xmas". This is just an example.
I've found a piece of code from a user of this site, dbenham, that does almost exactly what I'm after. In this example he's replacing "120x90" in a filename to "67x100"
Here's the code:
#echo off
setlocal enableDelayedExpansion
for %%F in (*120x90.jpg) do (
set "name=%%F"
ren "!name!" "!name:120x90=67x100!"
)
Can anyone help me adapt this code to make it handle spaces in the file name?
Thanks
All you're missing is quotes within the FOR statement - to follow your example:
#echo off
setlocal enableDelayedExpansion
for %%F in ("Family Christmas*.jpg") do (
set "name=%%F"
ren "!name!" "!name:Family Christmas=Photos - Xmas!"
)
I have the following code:
#echo off
setlocal disableDelayedExpansion
set "src=C:\test"
set "dst=C:\test2"
set "search=test"
for /r "%src%" %%F in (*%search%*) do (
set "full=%%~fF"
set "name=%%~nxF"
setlocal enableDelayedExpansion
copy "!full!" "%dst%\!name:%search%=test - %date:~0,2%-%date:~3,2%-%date:~6,4%__%time:~0,2%-%time:~3,2%-%time:~6,2%!"
endlocal
)
what this code does is copy a file from 1 location and put it on other location and change the name and put date/time in the filename.
Where i was looking for is that behind the filename and time also comes the location of the copyed file. so something like this:
Filename-10-03-2014-15:58:45-C:\test\test1\testfile.txt
so i can see the date time and old path in my filename.
Hope you guys can help.
Kind regards,
Kaluh
#echo off
setlocal disableDelayedExpansion
set "src=C:\sourcedir"
set "dst=C:\destdir"
set "search=jpg"
for /r "%src%" %%F in (*%search%*) do (
set "full=%%~fF"
set "name=%%~nxF"
setlocal ENABLEDELAYEDEXPANSION
SET "appendix=!full::=-!"
SET "appendix=!appendix:\=_!"
ECHO copy "!full!" "%dst%\!name:%search%=test - %date:~0,2%-%date:~3,2%-%date:~6,4%__%time:~0,2%-%time:~3,2%-%time:~6,2%!-!appendix!"
endlocal
)
GOTO :EOF
I changed the search pattern, source and destination directories to suit my system.
You would need to nominate your own substitute characters for : and \ which cannot appear in a filename.
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO COPY to COPY to actually copy the files.
I have a folder that gets a new file added everyday to the folder with the same file name but incremental extension such as .001, .002, .003, etc. However, if there's no file within the folder it starts at .001 again.
The problem is they are all named the same and if I move them to another folder to archive them it would just overwrite the same file over and over again. I could create a folder each day with the date with only one file in it, but that seems a bit redundant.
Is there a way to look at the create date of each file and rename it to the create date?
I've gotten this far, but it looks like for this situation I have to use a static file name, how to loop through the entire directory?
SET filename = C:\test.001
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
rename c:\test.001 C:\test_%filedatetime%.txt
move C:\*.txt C:\archive\
this provides the correct sort order:
#echo off &setlocal disableDelayedExpansion
set "startfolder=%userprofile%\test"
cd /d "%startfolder%"
for %%a in (*) do (
for /f "delims=." %%b in ('wmic datafile where "name='%startfolder:\=\\%\\%%~a'" get lastmodified^|find "."') do (
echo(ren "%startfolder%\%%~a" "%%~b.txt"
)
)
Remove echo to get it working.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetdir=c:\sourcedir"
SET "destdir=c:\destdir"
PUSHD "%targetdir%"
FOR %%a IN (*.*) DO (
SET "timestamp=%%~ta"
SET "timestamp=!timestamp:/=_!
SET "timestamp=!timestamp::=_!
SET "timestamp=!timestamp:.=_!
SET "timestamp=!timestamp:,=_!
SET "timestamp=!timestamp: =_!
ECHO MOVE "%%a" "%destdir%\%%~na.!timestamp!"
)
GOTO :EOF
This should work with any file in the nominated target directory where the name does not include ! or ^.
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
The gymnastics around timestamp are intende to replace /: with _ since these are illegal filename characters. Space., are similarly replaced - they're legal but often painful.
If you want the destination filename to be name.003.timestamp, remove the ~na from the destination name.
Try like this :
SET $path=The_path_who_contain_the_FILES
FOR /F "DELIMS=" %%f IN ('dir "%$path%" /a-d/b') DO (
SET filedatetime=%%~tf
move "%%~dpnxf" "C:\archive\test_%filedatetime%.txt")
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!
)