Rename files in directory in batch - batch-file

Can anyone help with a bat file to rename files in a windows directory from e.g.
this-image-file-name (1).tif
this-image-file-name (2).tif
this-image-file-name (3).tif
that-pic-file-name (1).tif
that-pic-file-name (2).tif
to
this-image-file-name-1.tif
this-image-file-name-2.tif
this-image-file-name-3.tif
that-pic-file-name-1.tif
that-pic-file-name-2.tif
basically strip out the space and parentheses and add in the hyphen?
#echo off
Setlocal enabledelayedexpansion
Set "Pattern= ("
Set "Replace=-"
For /R %%a in (*.tif) Do (
Set "File=%%~a"
Ren "%%a" "!File:%Pattern%=%Replace%!"
)
Set "Pattern=)"
Set "Replace="
For /R %%a in (*.tif) Do (
Set "File=%%~a"
Ren "%%a" "!File:%Pattern%=%Replace%!"
)
Pause&Exit
without the /D /r it works fine in the current folder but i need it to work on all subfolders

I commend to your attention the output of FOR /? or HELP FOR, or alternatively the documentation for FOR at SS64. The /D switch instructs the command to process Directories, ignoring files (that is, it would cause this batch file to attempt to rename directories, which is not what you want). If you are always going to start this batch file in the directory where the files to be renamed can be found, then /R and /S are equivalent; you must use /R if you wish to pass a starting folder to the command (see FOR /R at SS64).
Your batch file should work as you desire if you remove the /D in both FOR statements.

Related

Batch / CMD script to delete folder but excluding more than one

In advance: I'm new to batch or programming in general so please explain as much as possible.
My problem:
I want to delete all folders not named X and Y in a directory Z (ex. D:\Test\z)
So let's say Z contains these folder:
backup
resources
project1
project2
project3
I'd need to exclude only backup and resources from deletion via a script.
I looked up multiple soultions for not deleting only one folder with a certain name but I don't know if it's possible for multiple values with Batch.
not tested:
#echo off
set "root_dir=C:\Z"
set "exclude_list=backup resources"
pushd "%root_dir%"
for /f "tokens=* delims=" %%# in ('dir /b /a:d^| findstr /v "%exclude_list%"') do (
echo rd /s /q "%%~f#"
)
if this looks ok delete the echo on the last line to activate deletion.
If all folders you want to delete are like project* you can try also :
for /d %%a in ("project*") do rd /s /q "%%~fa"
#echo off
setlocal EnableDelayedExpansion
rem Define working variables
set "dir=D:\Test\z"
set "exclude=/backup/resources/"
rem Change current dir to working dir
cd /D "%dir%"
rem Process all folders in this dir
for /D %%f in (*) do (
rem If current folder is not in "exclude" var
if "!exclude:/%%f/=!" equ "%exclude%" (
rem Delete it
ECHO rd /s /q "%%f"
)
)
This method use internal cmd.exe commands only, so it run faster than other methods that may use external .exe files (like findstr.exe).
The way to detect if a name is in the exclude variable is trying to delete such name from it: "%exclude:/%%f/=%": if the result is equal to the original variable contents, the folder was not there. This method is very simple and efficient and works not matter the case of the letters, so it don't requires any /I ignore case switch in the if command.
The names are delimited by slashes to avoid any problem caused by partial name matches; for this reason the %%f part is enclosed in slashes in the if command.
Note that the value of %%f change in each iteration of the for command. For this reason, the exclude variable is surrounded by exclamation marks instead percent signs and the setlocal EnableDelayedExpansion command is given at beginning; otherwise, the %expansion% would be done just one time, before the for command start iterations. You may look for "delayed expansion" in this forum for a further explanation of this point.

rename all files in a folder with batch file

Can I rename all files in a folder with a batch file I tried this but it doesn't work
for %%a in (*.*) do ren sdel%random%.sdel %%a
and I also tryed
ren *.????? sdel%random%.sdel
Neither work what am I doing wrong?
Try this:
#echo off
setlocal EnableDelayedExpansion
for %%a in (*.*) do (
if not "%~nx0"=="%%a" ren "%%a" "sdel!random!.sdel"
)
Within a for loop, you should use EnableDelayedExpansion and use ! instead of % for variables. Note that I also added a if check so you don't rename the batch-file itself. If you wouldn't do that it would rename the batch-file, then be unable to find itself, and not rename any other files.

Copy all ini from one folder for all users to another folder

I am trying to copy all .ini files from the Windows folder into a new folder I have already created. I need this to loop through for all users. Here is what I have but it only works for the last user, not each of them. This is a batch file.
for /r  %%f  in ("D:\Home\*.*\windows") do
set dir="%%d
for /r "%dir%\windows\" %%f in (*.ini) do (
copy %%f "%dir%\temp_ini"
))
pause
Please help :/ Thank you
You've got a few problems -- unterminated quote on the second line, not delaying the expansion of %dir% (and indeed, setting %dir% is unnecessary, anyway), illogical use of for /r in the first line, trying to recycle %%f in nested loops, and your *.* wildcard in the first line will only match directories containing a dot. You should also make sure the temp_ini directory is outside the scope of your search for ini files; otherwise, Windows will attempt to copy the contents of temp_ini\*.ini into itself recursively. Try this instead:
for /d %%I in ("D:\Home\*") do (
rem // create directory if not exist
if not exist "%%~I\temp_ini" md "%%~I\temp_ini"
rem // capture the output of dir /s /b
for /F "delims=" %%x in (
'dir /s /b "%%~fI\Windows\*.ini" 2^>NUL'
) do copy /y "%%~fx" "%%~I\temp_ini\"
)
pause

How to rename a set of pdf files using a batch script

I know that there are several posts adressing this issue already. However I can't get my little batch script to work and I am a newbie so I would be very pleased if u could help me to solve that.
I have a bunch of pdf files named with a random number and "_text" e.g. 174098_text.pdf. Now I want to rename the file such that I only have 174098.pdf left (remove _text).
Here is my latest version of my file "Rename.cmd":
#echo off
#setlocal
REM +++++++++++++++++++++++++++++++++++++++++++++++++
REM ++++++++ Umbenennen von Dateien ++++++++++++++++
REM +++++++++++++++++++++++++++++++++++++++++++++++++
REM +++ Dateinamen und Pfad ermitteln
FOR /f "delims=" %%D in ('Dir /b %Path%\*_*.pdf') do (
FOR /f "delims=_ tokens=1-2" %%I in ('%%D') do (
ren %%D %%I.pdf
)
)
Endlocal
I hope you can help me and explain me what i have done wrong. Running the code it opens all the files but dosn't rename a single one of it.
/F parameter is for OPENING commands and/or get the output of a command, so when you use the second for /F you are telling the CMD to execute "%%D" file, so can't work that.
You can use a FOR without parameters, or a /R parameter if you need recursively a folder.
And you don't need to start setlocal for this job
#echo off
FOR %%# in ("C:\Folder\*.pdf") DO (
:: Set the "filename.extension"
Set "File=%%~nx#"
:: Rename it
Call Rename "%%#" "%%FILE:_TEXT=%%"
REM Explanation:
REM Rename "filename_text.pdf" with "Filename(_Text=NOTHING).pdf"
REM (That removes the "_TEXT" pattern in each filename)
)
pause&Exit
Remember, if you need recursive:
FOR /R "C:\Folder\" %%# in (*.pdf)

how to write bat file to process all files include sub directory, and output them with the same directory structure to another directory?

I have a bat file to process all file under a directory, and output to another directory
this is the codes in the bat file
#set dir1=mulit-simp\
#set dir2=mulit-trad\
#cd /d %~dp0
#if not exist %dir1% md %dir1%
#if not exist %dir2% md %dir2%
#for /f "delims=" %%i in ('dir %dir1%*.* /b') do #opencc.exe --input="%dir1%%%i" --output="%dir2%%%i"
#echo.
#echo Done!
#echo.
#pause
but this code can't process files in the sub directory
How could I process the sub directory files, and output them with the same directory structure to another directory?
thx for help :)
The DIR /S option would give you the entire folder hierarchy. And you also want the /A-D option to suppress folders from the output. Type HELP DIR or DIR /? for more info.
However, instead of using FOR /F with DIR, I recommend using FOR /R. Type HELP FOR or FOR /? for more info. Also see http://judago.webs.com/batchforloops.htm for easier to understand explanation of the FOR command.
Either way, you also need to create all of the destination folders. The easiest way to do that is to use XCOPY /T /E to create the destination folders prior to processing the files.
Some additional tips:
Simply put #echo off at the top instead of prefixing every command with #
ECHO. has been widely used for years, but it can actually fail to work properly. Safer to use ECHO( instead.
You probably don't want the environment variable you define to hang around after the batch is finished. Putting SETLOCAL at the top will make the variables temporary and they will exist only until the batch file finishes, or until ENDLOCAL is executed. Given that you are using PAUSE at the end, I suspect you are running the batch file by double clicking on the file or a shortcut. If so, then the command window closes at the end, and the variables disappear along with it. Then SETLOCAL is not needed, but it is still probably a good practice to get into anyway.
Here is the finished code with all of the suggestions. EDIT - The original code did not work. Altered to address tiance's comments
#echo off
setlocal
set dir1=mulit-simp
set dir2=mulit-trad
cd "%~dp0"
:: Replicate tree
if not exist "%dir1%" md "%dir1%"
if not exist "%dir2%" md "%dir2%"
xcopy /t /e "%dir1%" "%dir2%"
:: Get length of absolute path of %dir1%
<nul set /p "=%cd%\%dir1%" >getLength.tmp
for %%F in (getLength.tmp) do set len=%%~zF
del getLength.tmp
:: Process the files
for /r "%dir1%" %%F in (*) do (
set "src=%%F"
setlocal enableDelayedExpansion
opencc.exe --input="!src!" --output="%dir2%!src:~%len%!"
endlocal
)
echo(
echo Done!
echo(
pause
Have you tried XCOPY sourcefolder destinationfolder /E ?

Resources