Looping through directories to extract pages from multiple pdfs - batch-file

I have multiple directories that contain one pdf in each. Im trying to extract page 1 from each pdf and keep it in the directory that the original pdf was in so i end up with the multiple directories now containing two pdfs.
I currently have this code using ghost scripts which works, but only if the pdfs are in the same directory as the batch file
echo on
for %%I in ("D:\files\input\" *.pdf) do "C:\Program Files\gs\gs9.20\bin\gswin64c.exe" -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="%%~nI_p1.pdf" -dFirstPage=1 -dLastPage=1 "%%I"
I cant see why all the directories in D:\files\input\ are not being looped through.

You are executing the command with two parameters,"D:\files\input\" and *.pdf.
You need to look for each *.pdf file. The accepted way is
for /f "delims=" %%I in ('dir /b /s /a-d "D:\files\input\*.pdf" ') do ...
directory, basic format, with subdirectories, but no directorynames.
However, this would execute the command against every *.pdf including the *_p1.pdfs (ie those already processed) so
for /f "delims=" %%I in ('dir /b /s /a-d "D:\files\input\*.pdf"^|findstr /v /i /e /L "_p1.pdf" ') do ...
would exclude those files by find those names that do not (/v) end (/e) with the literal (/L) "_p1.pdf" without regard to case (/i). It would however regenerate the _p1s.
for /f "delims=" %%I in ('dir /b /s /a-d "D:\files\input\*.pdf" ') do if not exist "%%~dp*_p1.pdf" ...
would process those *.pdfs that have not already been processed.
(not tested)

Your usage of the for loop is the problem. See for /?
Magoo was faster, see this complete solution.
#Echo off
Set "GS=C:\Program Files\gs\gs9.20\bin\gswin64c.exe"
Set "Opt=-dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dFirstPage=1 -dLastPage=1"
Pushd "D:\files\input\"
for /f "delims=" %%I in (
'Dir /B/S *.pdf ^|find /v "_p1.pdf" '
) do If not exist "%%~dpnI_p1.pdf" "%GS%" %Opt% -sOutputFile="%%~dpnI_p1.pdf" "%%~fI"
Popd

Related

Piping string with folders in variable to findstr - copy and rename files

I'm trying to pipe a string that contains folder paths to findstr to search for a particular part in the names of the given folders - or at least, that is what I'm planning to do.
I've got a source folder with files that have to be copied into multiple subfolders and after that, one of the copied files has to be renamed corresponding to the destination folder. If there already are files with the same names, they have to be overwritten. I am trying to achieve this via .bat-file using the following commands in my code:
pushd ..\..\destination_folder\
FOR /F "delims=" %%i in ('dir /AD /S /B^| findstr /I "._Modul_X$"') do copy ..\xxx\yyy\ressources\*.* %%i
& ren %%i\xxxx_Modul_X.BAT_TEMPLATE" "%%i_Modul_X.BAT_TEMPLATE
The copy-part seems to work, the rename-part does not and when it comes to overwriting the one file that has to be renamed after copying it (name conflict!), I'm pretty clueless how to do this (IF EXIST & DEL?).
If I understand the process you're attempting, then the following should do as you require, subject to my assumption that xxxx is a sequence of exactly four, digits (directory names), and characters (file names):
#Echo Off
SetLocal EnableExtensions
PushD "..\..\destination_folder" 2>NUL || GoTo :EOF
If Not Exist "..\xxx\yyy\resources\*.*" GoTo :EOF
For /F "Delims=" %%G In (
'Dir /B /S /A:D "????_Modul_X" 2^>NUL ^|%__AppDir__%findstr.exe^
/I /R "\\[0123456789][0123456789][0123456789][0123456789]_Modul_X"'
) Do (
For /F "Delims=" %%H In (
'%__AppDir__%where.exe /F "%%G":"????_Modul_X.BAT_TEMPLATE" 2^>NUL'
) Do Del /A /F %%H
Copy /Y "..\xxx\yyy\resources\*.*" "%%G" 1>NUL
For /F "Delims=" %%I In (
'%__AppDir__%where.exe /F "%%G":"????_Modul_X.BAT_TEMPLATE" 2^>NUL'
) Do If /I Not "%%~nxI" == "%%~nxG%%~xI" Ren %%I "%%~nxG%%~xI"
)
Please note that you will probably need to modify both instances of xxx\yyy\resources, (lines 5 and 14), as nobody really uses names like that, paying special attention to the spelling, I've used resources above, not ressources.

Windows Batch How to delete specific words from txt file

I can't figure out how to remove a piece of text in a line.
I have a batch file that creates a list of files of interest to me in the selected folder.
dir D:\pool\template_test>U:\Desktop\list.txt
findstr "Work_T" U:\Desktop\list.txt > U:\Desktop\tamplates.txt
Output:
2013-03-13 17:24 622 Work_T_tamplate1.fdf
In final file, as you can see, presents the date, time, size, but I only need the name. How can I do this?
I tried to use the example from the other post, but it does not work:
for /f "tokens=3" %%A in (U:\Desktop\tamplates.txt) do findstr /C".dot" /C".pdf" /C".fdf" %%A
final file after using proposed code
So you already got an answer in comments by Stephan
These will include the search for you, the following only returns the filename:
cd /d "D:\pool\template"
(#for /f %%i in ('dir /b "Work_T*.dot" "Work_T*.pdf" "Work_T*.fdf" ') do (
#echo "%%i"
)
)>"U:\Desktop\tamplates.txt"
You could however also want the full path, so this would include the full path of the file:
cd /d "D:\pool\template"
(#for /f %%i in ('dir /b "Work_T*.dot" "Work_T*.pdf" "Work_T*.fdf" ') do (
#echo "%%~fi"
)
)>"U:\Desktop\tamplates.txt"
add /s to recurse through subfolders if needed.
Edit
for UNC paths:
(#for /f %i in ('dir /b /a-d "\\tuesrna02\pool\template" ^| findstr /i "Work_T"') do (
#echo %%i
)
)>"U:\Desktop\tamplates.txt"

Batch script to delete files in folder that does not contain certain word

I am new to batch scripting . I need to delete all files in a folder that DOES NOT contains some word in the file
found this code
#echo off
setlocal
pushd C:\Users\admin\Desktop\bat
findstr /ip /c:"importantWord" *.txt > results.txt
popd
endlocal
So how i can WHITE list this files, and delete all other?
Or i think there is easy way with just check if !contains and delete
but i don`t know how?
Supposedly, this problem could be solved in a very simple way combining these findstr switches: /V that show results when the search string is not found, and /M that show just the name of the files; that is:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
for /F "delims=" %%a in ('findstr /ipvm /c:"importantWord" *.txt') do del "%%a"
Unfortunately, the combination of /V and /M switches don't properly work: the result of /V is based on lines (not files), so a modification in the method is needed:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
rem Create an array with all files
for %%a in (*.txt) do set "file[%%a]=1"
rem Remove files to preserve from the array
for /F "delims=" %%a in ('findstr /ipm /c:"importantWord" *.txt') do set "file[%%a]="
rem Delete remaining files
for /F "tokens=2 delims=[]" %%a in ('set file[') do del "%%a"
This method is efficient, particularly with big files, because findstr command report just the name of the files and stop searching after the first string match.
#echo off
setlocal
set "targetdir=C:\Users\admin\Desktop\bat"
pushd %targetdir%
for /f "delims=" %%a in ('dir /b /a-d *.txt') do (
findstr /i /p /v /c:"importantWord" "%%a" >nul
if not errorlevel 1 echo del "%%a"
)
popd
endlocal
Not really sure what you want to do with /pfiles - files containing non-ansi characters appear to return errorlevel 1for these. if not errorlevel 1 will echo the files that do not contain the required string - remove the echo to actually delete the file(s)
This should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "pathToFolder=C:\FolderToEmpty"
SET "wordToSearch=ImportantWord"
FOR /F "tokens=*" %%F IN ('dir %pathToFolder% /b *.txt') DO (
findstr /IP %wordToSearch% "%pathToFolder%\%%F">nul
IF !ERRORLEVEL!==1 (
DEL /Q "%pathToFolder%\%%F"
)
)
You will have to set the proper path to the folder you want to delete the files from and to replace ImportantWord with the substring you are looking for.

In batch, how to list files of a specific directory?

I need to go through all the directories except one (named "FORBIDDEN"), and to print for each of them all the files they contain.
So I wrote a batch script like this:
#echo off
for /f "tokens=*" %%G in ('dir /b /s /a:d %cd%') do ^
if %%G NEQ C:\Users\ME\FORBIDDEN (dir /a-d %%G)
But the part (dir /a-d %%G) is not good because I get some errors saying that files were not find.
So, for each round of the loop, how to list all files present in the directory (whose path is in %%G) ?
Cheers
for /d /r %%d in (*) do if not "%%~nxd"=="FORBIDDEN" 2>nul dir /a-d "%%d"
For each folder if it is not the excluded one, show its contents
edited to adapt to comments
To only include the files with full path
for /d /r %%d in (*) do if not "%%~nxd"=="FORBIDDEN" (
for %%f in ("%%~fd") do echo "%%~ff"
)
Another option (that also includes files in the current folder) could be
dir /a-d /s /b | find /v "\FORBIDDEN\"
Get the full list and filter it, to only retrieve the lines that does not make reference to the excluded folder

BATCH Delete oldest backup file when creating a new one

I have a question regarding deleting the oldest file after a folder gets an X amount of files with the same extension, in this case, all the files share in common the extension *.bak, they are small files that I create for my firefox RES, they have imprinted on the title, the date and hour of creation, and that's as far as I can go.
Anyways, I've stumbled across this: Batch Script to delete oldest folder in a given folder. And I'm struggling to get it to work on my idea.
The thing is that I want the batch to simply check which file is the oldest after it creates a new one using this simple line of code.
copy /y store.json "%DROPBOX%\RES BACKUPS\store.json %date:~-4,4%-%date:~-7,2%-%date:~-10,2%_%time:~0,2%hs-%time:~3,2%min-%time:~6,2%s.bak"
You can use this:
#echo off
for /f "delims=" %%a in ('dir /b /a-d /t:w /o:d "%DROPBOX%\RES BACKUPS\*.bak"') do (
del "%%a"
goto :breakLoop
)
:breakLoop
I suggest first testing it with echo del "%%a" to make sure it deletes the right file.
This works by getting the output of the dir command, which shows all files in bare format (only filenames, not sizes etc.) sorted by oldest files first. It then deletes the first file found, and breaks the loop.
A version that keeps deleting files while there are more than a specific amount:
#echo off
set "source=%DROPBOX%\RES BACKUPS\*.bak"
set "minFiles=5"
for /f %%A in ('dir "%source%" /a-d-s-h /b ^| find /v /c ""') do if %%A leq %minFiles% goto :eof
:loop
for /f "delims=" %%a in ('dir /b /a-d /t:w /o:d "%source%"') do (
del "%%a"
goto :breakLoop
)
:breakLoop
for /f %%A in ('dir "%source%" /a-d-s-h /b ^| find /v /c ""') do if %%A gtr %minFiles% goto :loop
pause
You can make this non-looping (but still only delete if there are more than 5) by removing the line for /f %%A in ('dir "%source%" /a-d-s-h /b ^| find /v /c ""') do if %%A gtr %minFiles% goto :loop

Resources