rename all files in a folder with batch file - 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.

Related

Rename files in directory in batch

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.

Rename files using batch file script

I want to rename the all PDF files in folder using batch script. for example i have 3 files in folder:-
anyfile.pdf
otherfile.pdf,
another.pdf
Now i want to rename file as bellow:-
PDF0.pdf
PDF1.pdf,
PDF2.pdf
i have fetch the files using this script:-
#ECHO OFF
SETLOCAL DisableDelayedExpansion
SET "r=%__CD__%"
FOR /R . %%F IN (*.pdf) DO (
SET "p=%%F"
SETLOCAL EnableDelayedExpansion
ECHO(!p:%r%=!
ENDLOCAL
)
pause
now i can rename please help me.
Thanks
Are you just looking for the command to rename files? Its ren. Look at http://ss64.com/nt/ren.html for more info.
FOR /R and the string replacement to get rid of the path seem unnecessary here, since you stay within one directory.
(generally, if you want to get of the path, just say %%~nxFwhich returns the Name and eXtension of %%F.)
you can perform arithmetics, ie. count a number up, with SET /A, so you could do simply
#ECHO OFF
setlocal enabledelayedexpansion
set i=0
FOR %%F IN (*.pdf) DO (
set /a i=i+1
ren %%F PDF!i!.pdf
)
pause

Change extension of specific filetype on current folder and subdirectories with .bat

I would like to rename all the .log as .ok from a particular folder and subdirectories
The following will usually work just fine:
#echo off
for /r "PathToYourFolderHere" %%F in (.) do ren "%%F\*.log" *.ok
But the above can have problems if short file names are enabled on your drive and you have extensions longer than 3 characters. It will also rename files like name.log2 because the short name will have an extension of .log.
The following will only rename true .log files:
#echo off
for /f "eol=: delims=" %%F in (
'"dir /b /s /a-d PathToYourFolder\*.log|findstr /lie .log"'
) do ren "%%F" *.ok
Note: The rules for how RENAME treats wildcards can be found at How does the Windows RENAME command interpret wildcards?
run a .bat file from the folder containing:
for /R %%x in (*.log) do rename "%%x" "%%~nx.ok"
/R for recursive
%%~nx for the filename without extension

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)

Creating Soft Links

I am putting together a script for my media server. I need to run through all the
files in a directory and then ultimately create softlinks in another folder to act as a video playlist. I'm just starting out and I'm already having problems with my batch script
#echo off
SetLocal EnableDelayedExpansion
set TV="G:\TV"
FOR /R %TV% %%G in (.) DO (
Pushd %%G
Echo now in %%G
for /f %%f IN ("dir /b") do (
Echo %%f
)
Popd )
Echo "back home"
)
This produces a listing for each file but the filenames cut out after any spaces! It looks something like this:
now in G:\TV\UCB\UCB Season 3\.
Upright
Upright
Upright
Upright
...
now in G:\TV\Venture Bros\Season 3\.
File Not Found
now in G:\TV\Venture Bros\Season 4\.
The.Venture.Bros.S04E03.HDTV.XviD-2HD.avi
The.Venture.Bros.S04E04.HDTV.XviD-2HD.avi
The.Venture.Bros.S04E05.HDTV.XviD-2HD.avi
What can I do to fix this? Once I have the file it should be easy to create a soft link.
Just add quotes around your file names:
Pushd "%%G"
you may replace all your BAT with a simple oneliner FOR loop.
FOR /R G:\TV %%f in (*) do echo create soft link for %%f

Resources