I am trying to write a batch script, which removes all non pdf files from a directory.
The directory name is OUTPUT/
this what i got
FOR /f %%f IN (.\OUTPUT) DO
SET fileName=%%f
IF NOT "!fileName:~-3!"=="pdf" (
DEL !fileName!
)
Although your question is simply that you're not using the correct syntax with your for command, there are other remarks I noted, which I decided would be better as an answer.
The most obvious one is that there is that you're setting a variable for no reason, and then having to use it with delayed expansion.
To do it as you were, it should look more like this:
#Echo Off
SetLocal DisableDelayedExpansion
For %%G In (Output\*) Do (
Set "fileName=%%G"
SetLocal EnableDelayedExpansion
If /I Not "!fileName:~-4!"==".pdf" (
Del /F "!fileName!"
)
EndLocal
)
These entire batch files should do the same thing.
Standard for loop:
#For %%G In (Output\*)Do #If /I Not "%~xG"==".pdf" Del /F "%%G"
Using the for /f variant:
#For /F "Delims=" %%G In ('Dir /B/A-D-S-L "Output" 2^>NUL^|FindStr /VILE ".pdf"')Do #Del /F "Output\%%G"
or over multiple lines, if you find it easier to read:
#Echo Off
For /F "Delims=" %%G In (
'Dir /B/A-D-S-L "Output" 2^>NUL^|FindStr /VILE ".pdf"'
) Do (
Del /F "Output\%%G"
)
Please remember that almost every windows cli command has built-in help, and the vast majority accept the /? option, e.g. for /?, del /?. Additionally you can use the help command, e.g. help dir, help findstr.
Related
I have went through a lot of guides for it, but havent found a way to do something similar to lets say turn all files in folder, like these files:
djhwu4s_cat_ruhg29.png
397y_dog_j0929_ej93.png
8yhh_owl.png
into these:
_cat.png
_dog.png
_owl.png
So basically removing everything from file names but a list of predefined strings i am searching for. In example above i would define list as "_cat", "_dog", "_owl". I know that each file will have only one of these variables, and there will be only one file with each of them in folder.
Will appreciate any tips on how to achieve that. Thanks in advance!
edit:
Here is what i came up with (with stuff i can understund) and what seems to be working fine now.
#echo off
setlocal enabledelayedexpansion
set v1=_cat-cat
set v2=_cat-owl
set v3=_cat
set v4=_dog
set v5=_owl
set v6=_horse
FOR /L %%a IN (1,1,6) DO (
rem echo %%a
rem echo !v%%a!
FOR /f %%f in ('dir /b /a:-D *!v%%a!.*') DO (
REN %%f !v%%a!.*
)
FOR /f %%f in ('dir /b /a:-D *!v%%a!_*.*') DO (
REN %%f !v%%a!.*
)
)
rem using two passes of this simpler code i can grasp and understund with dot and with underscore
rem after constructed variables value i make sure cat-cat is not recognised as and renamed to cat
rem no matter if im getting file with that variable as the last string before extension or another underscore
rem Gonna test it in combat now
For some reason this stuff doesnt work with files containing spaces and characters like:
"ab’c efg_dog.png"
FOR /L %%a IN (1,1,36) DO (
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
REN "%%f" "!v%%a!.*"
)
FOR /f %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
REN "%%f" "!v%%a!.*"
)
)
After further testing i have realised the problem starts with the %%f, and not the REN function as i thought. echo %%f before ren gives just the first part of the name to the first space, hence the REN function cant find the file. In case of "ab’c efg_dog.png" after finding the file with dir, the %%f becomes just "ab’c".
edit: After more tests and experiments and adding those "delims" to the code, the echo now shows proper, full names to be renamed, but it replaces that weird ’ character with ' for the REN command and thats why it still cant find the file to rename.
FOR /L %%a IN (1,1,36) DO (
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
FOR /f "delims=" %%f in ('dir /b /l /a:-D *!v%%a!_*.*') DO (
echo %%f
echo REN "%%f" "!v%%a!.*"
)
)
#ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
PUSHD "%sourcedir%"
FOR /f "delims=" %%e IN ('dir /b /a-d "*_*" 2^nul ^|findstr /v /b "_"') DO (
FOR /f "tokens=2delims=_." %%y IN ("%%e") DO ECHO REN "%%e" "_%%y%%~xe"
)
POPD
GOTO :EOF
For lack of examples, here's a start.
Always verify against a test directory before applying to real data.
Process the list of filenames that match *_*; find those names that do not start _, pick the second string between the delimiters _ and . and rename using that string (in %%y), prefixed by _ and appended with the original extension.
The ren command is simply echoed to the screen for verification. When happy, remove the echo before the ren to actually execute the rename.
I have a BATCH file which deletes every other JPG file. I am looking to make it so that when it is run, it affect all subfolders as well.
I am not a coder by all means, so apologies if this is a rookie question!
Below is my current code:
#echooff
setlocal
for /r %%D in (.) do (
set "del="
for /f %%F in ('dir /b "%%D\*.jpg"') do if defined del (
del "%%D\%%F"
set "del="
) else set "del=1"
)
PAUSE
The /S switch in the dir makes you go through subdirectories, as you can see here:
dir /?
...
/S Displays files in specified directory and all subdirectories.
...
So I'd advise you to replace:
dir /b "%%D\*.jpg"
by:
dir /S /b "%%D\*.jpg"
Good luck
Based upon your clarifications, is this what you're trying to achieve:
#SetLocal EnableExtensions DisableDelayedExpansion
#For /F "EOL=?Delims=" %%G In ('Dir /B/S/A:D 2^>NUL') Do #(Set "_="
For /F "EOL=?Delims=" %%H In ('Dir /B/A:-D/O:N "%%G\*.jpg" 2^>NUL')Do #(
If Not Defined _ (Set "_=T")Else Del /A/F "%%G\%%H"&Set "_="))
I try to improve an overview of my search and find memories for batch files in Windows XP command prompt environment.
In order to my previous sentence I am not happy with my search possibilities and have to post a question.
I try to compare the names of some text files and have written words in a text file that are by reading the same. With such a start environment I wrote following batch script to get an echo output.
The aim is
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%b in ('dir "C:\A Folder"') do set var=%%~nb & echo !var!
rem The output is the name of the files without extension. Now my question:
rem Is it possible to compare the above file names with some input
rem from a text file, for example like:
for /f "delims=" %%b in ('dir "C:\A Folder"') do set var=%%~nb & for /f %%a in (Textfile.txt) do (if !var!==%%a echo good else echo search)
rem That returns no output. I would like to know if there are possibilities
rem to do that? And if it is possible, how to revise this batch file?
endlocal disabledelayedexpansion
pause
Have a nice day, wishes
Stefan
This should work with Latin characters - some foreign characters may not work:
#echo off
for /f "delims=" %%b in ('dir /b /a-d "C:\A Folder\*.*" ') do find /i "%%~nb" < "textfile.txt" >nul && (echo "%%~nb" found) || (echo "%%~nb" not found)
pause
proper formatting you code increases readabilty:
for /f "delims=" %%b in ('dir /b /a-d "C:\A Folder"') do (
for /f %%a in (textfile.txt) do (
if "%%~nb"=="%%a" ( echo good ) else ( echo search )
)
)
I added a /b to the dircommand (show name only, no date/time/attributes) and a /a-d to exclude directorynames.
You don't need to use a variable (!var!) here (but you can, it works fine).
I want to create a 0 byte file names dblank in a specific directory C:\Users\myUser\*.data\.
echo. 2>"C:\Users\myUser\*.data\dblank.txt"
The * sign in the above command refers to any letters or numbers. I do not know. How can I refer to any letters or numbers in my batch code?
Maybe this:
setlocal enableextensions
for /D %%i in (C:\Users\myUsers\*.data) do copy nul "%%~i\dblank.txt"
endlocal
You can omit setlocal/endlocal if command extensions are already enabled (cmd /E:on).
This works on every existing *.data folder, if any.
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data') do echo. 2>"%%f\dblank.txt"
EDIT
Filter results:
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data^|findstr /r "\\[0-9a-zA-Z]*\.data$"') do (
echo. 2>"%%f\dblank.txt"
)
I need to include page size information of many single-paged pdf's into their filenames. E.g. "150x250mm.pdf". I found no renamer apps able to do it. I suspect this could be done using a batch file and pdfinfo.exe (from xpdf suite), but I have no idea how to make use of it.. Could you give me some hints?
Yes, you can convert from postscript points to MM. In this case, the script is in the top level folder containing the PDF's to be renamed. It does go into subfolders. If you don't want or need that, remove the /s from the dir command on the 5th line. Change the paths as needed.
#echo off
setlocal enabledelayedexpansion
set "pdfi=U:\Scripts\Utilities\xpdf\pdfinfo.exe"
for /f "delims=" %%a in ('dir /b /s *.pdf') do (
for /f "tokens=3,5 delims= " %%b in (
'%pdfi% -meta "%%a"^|find /i "Page size:"') do (
set pts=%%b %%c
for %%d in (!pts!) do (
call :Eval %%d*.352777778 mm
set "mm1=!mm1!x!mm!"
)
ren "%%~dpfnxa" "!mm1:~1!.pdf"
set mm1=
)
)
exit /b
:Eval <in> <out>
setlocal
if exist eval.vbs del eval.vbs
>eval.vbs echo wsh.echo formatnumber(eval("%1"),0)
for /f "delims=" %%a in (
'cscript //nologo eval.vbs'
) do endlocal & set %~2=%%a
del eval.vbs