Batch Script merging PDF's with pdftk - batch-file

Thanks in advance for any help given.
After searching through all relative threads and google search I'm stumped on finding a solution to output a variable name for merging two PDF's.
So I have 100's of PDF's I need to combine (two at a time) in a folder c:/test
The files are set out like below
Company Name Invoice No 123456
Company Name Invoice No 123456 details
Now I have managed to move two files at a time to a different folder and merge them but can't seem to get the desrired output name I'm after which is to put a week ending date in front (or at the end, not fussed) of the first merged filename. Below is the code I have thus far which works but the output file name is blank but gets created.
Very new to batch scripting and would appreciate any help :)
#echo off
setlocal enableextensions enabledelayedexpansion
set pdftk=C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe
set Source=C:\test
set Target=C:\test\test2
set num=2
set filenumber=1
for /F "tokens=1,2 delims=:" %%f in ('dir /b /a-d "%source%\*.pdf" ^| findstr /n "^" ') do (
if %%f leq %num% (
copy "%source%\%%g" "%target%" /y > nul
) else goto endCopy
)
:endCopy
endlocal
for /F "tokens=1,2 delims=:" %%f in ('dir /b /a-d "%target%\*.pdf" ^| findstr /n "^" ') do (
if %%f leq %filenumber% ( set file=%%~nA
)
)
pdftk *.pdf cat output we_19_9_2017_%file%.pdf

In endCopy you are trying to get the name of A whereas you are iterating with f. Use set file=%%~nf to set the name of file or set file=%%~ng for second file.
And move endlocal at the end to expand !file! at the end of script like this (note the !):
:endCopy
set "cmd=dir /b /a-d "%target%\*.pdf" ^| findstr /n "^""
for /F "tokens=1,2 delims=:" %%f in ('%cmd%') do if %%f leq %filenumber% set file=%%~nf
pdftk *.pdf cat output we_19_9_2017_!file!.pdf
endlocal
Read more about DelayedExpansion at: https://ss64.com/nt/delayedexpansion.html

The last command doesn't use the target folder for the input files and thus looks for the input files in the current folder, so either include the path or first change to the target path.
Also you set a path-variable for pdftk but don't use it.
If this path isn't included in the %path% it can't be find.
Try this (untested)
#echo off
setlocal enableextensions enabledelayedexpansion
set "pdftk=C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe"
set "Source=C:\test"
set "Target=C:\test\test2"
set num=2
set filenumber=1
for /F "tokens=1,2 delims=:" %%f in (
'dir /b /a-d "%source%\*.pdf" ^| findstr /n "^" '
) do if %%f leq %num% (
copy "%source%\%%g" "%target%" /y > nul
) else goto endCopy
:endCopy
endlocal
for /F "tokens=1,2 delims=:" %%f in (
'dir /b /a-d "%target%\*.pdf" ^|findstr /n "^" '
) do if %%f leq %filenumber% set file=%%~nf
PushD "%Target%"
"%pdftk%" *.pdf cat output we_19_9_2017_%file%.pdf
PopD

Related

Link a wild card to a script in Batch

Hello I was told to ask another question
I would like to see how to add this line to my current script
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%source%\(1)*.txt"') DO (
Or if I can get the help updating "File" and "Filename" to this (1) *.txt this would great as well
#ECHO OFF
SETLOCAL
ECHO STARTING 1
For %%G In ("%~dp0..\New Folder 1") Do Set "source=%%~fG"
For %%G In ("%~dp0..\New Folder 2") Do Set "target=%%~fG"
For %%G In ("%~dp0..\New Folder 3") Do Set "destdir=%%~fG"
set "FILE=(1) Homes in Texas.txt"
set "FILENAME=(1) Homes in Texas.txt"
for /f "tokens=1 delims=[]" %%a in ('find /n "appi"^<"%source%\%file%"') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "opcn"^<"%source%\%file%"') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%source%\%file%"') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>"%target%\(A)_sets.txt"
for /f "tokens=1 delims=[]" %%a in ('find /n "paid"^<"%source%\%file%" ') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "whbn"^<"%source%\%file%" ') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%source%\%file%" ') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>"%target%\(B)_sets.txt"
If Exist "%target%\*.txt" If Exist "%destdir%\" (
Copy /Y /B "%target%\(A)_Sets.txt" + "%target%\(B)_Sets.txt" "%destdir%\(1).txt"
)
ren "%destdir%\(1).txt" "%filename%"
If Exist "%source%\(2) *.txt" (
call "%~dp0(2) Extraction tool.bat"
)
GOTO :EOF
Now I would like to keep this script the speed on my script is very fast it can edit my files at 1 sec per file
I'm OK with using a partial Filename
Because of my setup it makes for no errors, it's super fast speed and all files have these in the filename (?)
With the way I have my setup my workstation, I would have to edit my scripts over and over to match the file name, so I'm trying to use these (1) *.txt, because all my txt file look like this
Sub Houston Folder
(1) Houston.txt
(2) Houston.txt
(3) Houston.txt
Sub Auston Folder
(1) Austin.txt
(2) Austin.txt
Sub Dallas Folder
(1) Dallas.txt
(2) Dallas.txt
(3) Dallas.txt
I have over 5000 files easy
They all have (?) in the file name so looking for (1) *.txt, but don't know if this will be an issue with ren "%destdir%\(1).txt" "%filename%" using (1) *.txt
If there is a way to fix my File and Filename to work on (1) *.txt and to keep the original file name as end result that would be very appreciated
Any Help will be great
Is this correct, when I set this up this way or any other way I get blank files [corrected code below. Stephan]
REM not here... set "FILE=%~1"
set "FILENAME=(1) Homes in Texas.txt"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%source%\(1)*.txt"') DO call :label "%%q"
goto :eof
:Label
REM here "%~1" gets the value "%%q" from the CALL command, a FQDN (filename including drive and path because of 'dir /s /b')
for /f "tokens=1 delims=[]" %%a in ('find /n "appi"^<"%~1"') do set /a start=%%a
My Setup Folders
Main Folder
|Extraction Batch Folder
|New Folder 1
|all original txt files
|New Folder 2
|all extracted txt files
|New Folder 3
|all renamed and merged files
It's now almost ready We got File fixed with that line
still working on Filename
If Exist "%target%\*.txt" If Exist "%destdir%\" (
Copy /Y /B "%target%\(A)_Sets.txt" + "%target%\(B)_Sets.txt" "%destdir%\(1).txt"
)
ren "%destdir%\(1).txt" "%filename%"
I know that wildcards does work with ren I tested this to confirm *
If Exist "%target%\*.txt" If Exist "%destdir%\" (
Copy /Y /B "%target%\(A)_Sets.txt" + "%target%\(B)_Sets.txt" "%destdir%\(1).txt"
)
ren "%destdir%\(1).txt" "(*)A.txt"
and in %destdir% it rename to (1)A.txt so the wild card sees the (1)
how can I set this up to take the matching (1) on the filename located in the source folder and use that to rename the (1).txt found in `%destdir% folder

Batch file Remove last charater on last loop item

I have batch file that gives-me all files inside a folder and creates a txt file with the file names, separated by comma ",". On the last loop i need the comma don't appear.
Results: DOc1,DOc2,DOc2,DOc2,DOc1,
This is What i need:(Whithout last comma) DOc1,DOc2,DOc2,DOc2,DOc1
I think i'm missing something on my code.
#echo off
<nul (
for /f "eol=: delims=" %%F in ('dir /b /o:n ^| findstr /vile ".bat .txt"') do set /p ="%%F, "
) >fileList.txt
Thanks for any help
You could use a method of determining whether or not the file is the first returned, adjusting the output accordingly. In this case, the findstr command you're using already, has a method of determining the line number, (its /N option):
#Echo Off
SetLocal EnableExtensions
< NUL (
For /F "Tokens=1* Delims=:" %%G In (
'Dir /B /A:-D /O:N ^| %__AppDir__%findstr.exe /VNLIE ".bat .txt"'
) Do If %%G Equ 1 (Set /P "=%%H") Else Set /P "=, %%H"
) > "fileList.txt"
Define a flag-like variable that indicates whether it is the first iteration and apply the separator conditionally:
#echo off
set "FIRST=#"
< nul (
for /F "eol=: delims=" %%F in ('dir /B /O:N ^| findstr /VILE ".bat .txt"') do (
if not defined first (set /P =", ") else set "FIRST="
set /P ="%%F"
)
) > "fileList.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.

Batch to get lines between two strings in multiple text files in subfolders

I have a series of text files each named the same in sub-folders of a certain directory
ac.txt files have the following structure :
---
some text
---
[lights]
---
some text
---
[GetEngineData]
---
some text
---
I want to get all those lines in between strings [lights] and [GetEngineData] (including those start [lights] and end [GetEngineData] lines) in one single output file called lights.txt with a blank space in between those coming from each text file.
I coded the following batch yet it is of no avail so far :
#ECHO OFF
for /r %%a in ('find /n "[lights]"^<(ac.txt) ') do set /a start=%%a
for /r %%a in ('find /n "[GeneralEngineData]"^<(ac.txt) ') do set /a end=%%a
(
for /r %%a in ('find /n /v ""^<(ac.txt) ') do (
IF %%a geq %start% IF %%f leq %end% ECHO(%%b
)
)>lights.txt
Here's a way to do it. Might not be the most efficient but it seems to do the job just fine. The code loops through all subfolders and picks up all .TXT files. It then parses each line of each file, marking the beginning/end of each block using the [lights] and [GeneralEngineData] tokens and then outputs everything to res.txt in the same folder where the batch file is stored.
#ECHO OFF
Setlocal EnableDelayedExpansion
if exist res.txt del res.txt
set inblock=0
for /r . %%a in (*.txt) do (
set fname=%%a
for /f "tokens=1* delims=]" %%b in ('type "!fname!" ^| find /n /v ""') do (
if /i *%%c*==*[lights]* set inblock=1
if !inblock!==1 (
if *%%c*==** (echo.) else (echo %%c)
if /i *%%c*==*[GetEngineData]* set inblock=0
)
)
echo.
) >> res.txt
set fname=
set inblock=
type res.txt

Batch file for counting the files with same initial name and moving them

Is it possible to do this?
In a folder i have files with same initial names
Example:
Main folder
-Quest2323231.txt
Quest2343434.txt
Quest2343435.txt
Fund103.txt
Fund102.txt
I have a config file (abc.config) in which i have name of file on which i need to count and move them . If the count is more than 2 then i need to move them.
In this case for e g I need to find files which have name as 'Quest'
Appreciate you help on this.
#echo off
setlocal enableextensions
set "number="
for /f "tokens=1" %%a in (
'dir /a-d /-c "c:\mainfolder\quest*" 2^>nul^|findstr /b /c:" "'
) do if not defined number set "number=%%a"
if not defined number set "number=0"
echo %number%
Untested: This expects text in the first line of abc.config which is the file information such as Quest in the example and if there are more than 2 matching files in the source folder then it will echo a move command to move them to the target folder.
Change *%file%* to %file%* in two places if you want to match only the start of the filename.
Remove the echo to actually perform the move commands.
#echo off
set "source=c:\mainfolder"
set "target=d:\target\folder"
set /p "file=" < "abc.config"
for /f %%a in ('dir /b /a-d "%source%\*%file%*" ^|find /i "%file%" 2^>nul^|find /c /v "" ') do set "number=%%a"
if %number% GTR 2 for /f "delims=" %%a in ('dir /b /a-d "%source%\*%file%*" ^|find /i "%file%" ') do (
echo move "%source%\%%a" "%target%"
)
pause

Resources