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
Related
What do I have to change here in this code so it could do also subfolders?
Or if it is easier to run only through subfolders?
#echo off
setlocal enableDelayedExpansion
for %%F in (*.jpg) do (
set "name=%%F"
ren "!name!" "!name:_=!"
)
This runs ok in current folder it erase in jpg filename character "_", but I don't know how to do it in subfolders, and that is my main goal to do.
It is possible to use a For /R loop:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /R . %%G In ("*_*.jpg") Do (
Set "name=%%~nxG"
SetLocal EnableDelayedExpansion
If Not Exist "%%~dpG!name:_=!" Ren "%%G" "!name:_=!"
EndLocal
)
I have used a . character after /R to signify the current directory as the recursion base, whilst this is not necessary, because the current directory is assumed if no path is provided, it serves as a reminder, the you could include another path there if needed.
Although you could also use a For /F loop, with the Dir command:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "EOL=? Delims=" %%G In ('Dir /B/S/A:-D "*_*.jpg"') Do (
Set "name=%%~nxG"
SetLocal EnableDelayedExpansion
If Not Exist "%%~dpG!name:_=!" Ren "%%G" "!name:_=!"
EndLocal
)
In this case, if you wanted to use a directory other than the current directory, you can insert it directly in the Dir glob, e.g. "C:\SomePath\*_*.jpg"
Please note however, in both cases, no attempt has been made to ensure that the remaining string, after removal of the underscores is a valid filename. It is your responsibility to incorporate such a check, if you wish to have robust code in your environment. Additionally no check is included to ensure that short filenames, (8.3), are not matched, so if this could be an issue in your target environments, then you should include modifications or additions to cater for that.
I am trying to use Tesseract-OCR to read and OCR all .png files, not only in current folder, (as there is answer for that) but also in all subfolders.
This works for folder:
for %%A in ("C:\Users\x\AppData\Local\Tesseract-OCR\temp\*.png") do C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "%%~fA" "%%~dpnxA"
I tried with this to go through all subfolders that I have in "temp" folder:
(for /r %%a in (*.png) do C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "%%~nxa" "%%~dpnxA")
but I got this errors for every file:
C:\Users\x\AppData\Local\Tesseract-OCR\temp>C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "01.png" "%~dpnxA"
Tesseract Open Source OCR Engine v4.1.0-elag2019 with Leptonica
Error, cannot read input file 01.png: No such file or directory
Error during processing.
It is obvious that the script finds all files in all of the subfolders, but then it cant read then for some reason?
Also, this script works for one folder, but when I try to use with /r it doesnt go through all subfolders:
:Start
#Echo off
Set _SourcePath=C:\Users\x\AppData\Local\Tesseract-OCR\temp\*.png
Set _OutputPath=C:\Users\x\AppData\Local\Tesseract-OCR\temp\
Set _Tesseract="C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe"
:Convert
For %%A in (%_SourcePath%) Do Echo Converting %%A...&%_Tesseract% %%A %_OutputPath%%%~nA
:End
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
Any ideas?
Perhaps this sort of thing is what you're looking for:
#Echo Off
SetLocal DisableDelayedExpansion
Set "_SourcePath=%LocalAppData%\Tesseract-OCR\temp"
Set "_SourceMask=*.png"
Set "_OutputPath=%LocalAppData%\Tesseract-OCR\temp"
Set "_TesserFile=%LocalAppData%\Tesseract-OCR\tesseract.exe"
For /F "Delims=" %%A In (
'""%__AppDir__%where.exe" /R "%_SourcePath%" "%_SourceMask%" 2>Nul"'
) Do Echo Converting %%A...& "%_TesserFile%" "%%A" "%_OutputPath%\%%~nA"
Note, this assumes that tesseract allows for specifying the output directory and accepts doublequoted strings etc. It also assumes that you intend for all output files to be placed in %_OutputPath%.
If you wanted them to be placed along side their respective .png's then perhaps this will do it:
#Echo Off
SetLocal DisableDelayedExpansion
Set "_SourcePath=%LocalAppData%\Tesseract-OCR\temp"
Set "_SourceMask=*.png"
Set "_TesserFile=%LocalAppData%\Tesseract-OCR\tesseract.exe"
For /F "Delims=" %%A In (
'""%__AppDir__%where.exe" /R "%_SourcePath%" "%_SourceMask%" 2>Nul"'
) Do Echo Converting %%A...& "%_TesserFile%" "%%A" "%%~nA"
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.
I am new to batch and this site. I searched for similar question and found some. But they are pointed towards having FART or other third party installations. But I need only batch script. Please help me in this.
I have multiple .txt files in a folder C:\files.
All the files have text '>>superman<<' in them.
I want to replace it with '>>batman<<' without creating any new files.
#echo off
SETLOCAL
for %%* in (.) do set foldername=%%~n*
SET stringtofindreplace=XXXX
for %%f in (*.fmw) do (
echo Processing %%f...
fOR /F "delims=" %%l IN (%%f) DO (
SET "line=%%l"
SETLOCAL ENABLEDELAYEDEXPANSION
set "x=!line:%stringtofindreplace%=%foldername%!"
echo(!x!
ENDLOCAL)
)>%%~nf.new
)
GOTO:EOF
Thanks in advance
I need some help with batch scripting. I need to remove a specific word from a file extension using a windows batch script. Etc
Error_test_qif,
Error_test2_qif,
Test3_error_qif,
I need to get the "error" string removed from the file name to look like below
Error_test_qif to test_qif,
Error_test2_qif to test2_qif,
Test3_error_qif to Test3_qif,
I tried using the ren* command but it only works with bulk renaming from one file extension to another. Help would be appreciated
This can probably be simplified, but you get the idea:
#echo off
setlocal enableDelayedExpansion
for %%F in (*_error*) do (
set "name=%%F"
ren "!name!" "!name:_error=!"
)
for %%F in (*error_*) do (
set "name=%%F"
ren "!name!" "!name:error_=!"
)
save as .bat or .cmd and run in same directory as your files.
(original posted code rolled back - addendum below retained)
Much as I am loath to edit an answer, sadly the edited answer now presented is an adaption and will generate an error report for files named "...error..." which the original did not do.
Here's an adaption of the original post: (press the Edited datetime link below the response text for history)
#echo off
setlocal enableDelayedExpansion
for %%F in (*_error* *error_*) do (
set "name=%%F"
SET "name=!name:_error=!"
ren "%%F" "!name:error_=!"
)
I believe this is the best editing result to represent the responses.
Now we have a change to the specification. The filenames posted were not filenames as assumed but extensions only. This should be fixed like this:
#echo off
setlocal ENABLEDELAYEDEXPANSION
for %%F in (*.*_error* *.*error_*) do (
set "ext=%%~xF"
SET "ext=!ext:_error=!"
ren "%%F" "%%~nF!ext:error_=!"
)
This works for you ...
#echo off
SETLOCAL enabledelayedexpansion
SET "word=Error_"
IF "%word%"=="" GOTO :EOF
FOR /f "delims=" %%a IN ('dir /a-d /b "*%word%*.qif"') DO (
SET "fname=%%~na"
SET "fname=!fname:%word%=!"
IF NOT "!fname!"=="" REN "%%~a" "!fname!%%~xa"
)