I have a text file which contains the location of a list of pdf files. I am writing a windows batch file that needs to read this line by line and append in a command which will be executed to merge all the pdfs into 1 pdf using pdftk.
Below is the example command:
pdftk "C:\test\1.pdf" "C:\test\2.pdf"......"C:\test\50.pdf" cat output merged.pdf
I came across this How do you loop through each line in a text file using a windows batch file? for reading text file.
But how do I read and append to a variable which can then be used for the command mentioned above?
Assuming your list of pdf files looks like this
pdf1.pdf
pdf2.pdf
pdf3.pdf
Then you can use this to concatenate them into one variable
setlocal enabledelayedexpansion
set files=
for /f "tokens=*" %%a in (pdfs.txt) do (
if defined files (
set files=!files! "%%a"
) else (
set files="%%a"
)
)
pdftk !files! cat output merged.pdf
The if else is there to remove the leading space from the variable, I wasn't sure if that would make a difference. If it doesn't then you can get rid of it and just use
setlocal enabledelayedexpansion
set files=
for /f "tokens=*" %%a in (pdfs.txt) do (
set files=!files! "%%a"
)
pdftk !files! cat output merged.pdf
Related
I have a batch file, which I will post below, I am trying to have it copy the first line in a file and then stores it in a variable.
#echo off
setlocal enabledelayedexpansion
set SEPARATOR=/
set filecontent=
for /f "delims=" %%a in ("MavenInstructions.txt") do (
set currentline=%%a
set filecontent=!filecontent!%SEPARATOR%!currentline!
)
echo The file contents are: %filecontent%
echo %filecontent%
pause
The first line of the MavenInstructions.txt is TEST LINE but what I get when I run my batch file is:
The file contents are: /MavenInstructions.txt
/MavenInstructions.txt
Press any key to continue . . .
for /f "usebackqdelims=" %%a in ("MavenInstructions.txt") do (
usebackq is required if the filename is "quoted".
btw - set filecontent=!filecontent!%SEPARATOR%%%a
would suffice.
I'm using this script to combine multiple csv files all with the same header and all in the same directory. It works well, but it writes the file path to the last line in MASTER.csv, which is an issue when I go to process the data. I'd like to append this batch file so that it no longer outputs the file path, otherwise how would I delete the last line in the MASTER.csv
#echo off
setlocal EnableDelayedExpansion
set first=1
set fileName="MASTER.csv"
>%fileName% (
for %%F in (*.csv) do (
if not "%%F"==%fileName% (
if defined first (
type "%%F"
set "first="
) else more +1 "%%F"
)
)
)
Example of the last three lines of MASTER.csv:
2014/05/31,23:58:00, 22.0C, 22.33C, 16.17,OK,OK,OK,OK,OK,OK,OK,OK,OK,Off
2014/05/31,23:59:00, 21.9C, 22.39C, 16.11,OK,OK,OK,OK,OK,OK,OK,OK,OK,Off
C:\Users\ME\Desktop\program\data\enviro\MASTER.csv
file.bat | find /v "C:\Users\ME\Desktop\program\data\enviro\MASTER.csv" > MASTER.csv
I've searched a thousand of example and tried, but none of them actually works for me. My requirement is pretty straight forward, I have a file - config.txt, it has one of lines:
sqlServer=localhost
I'm trying to update this line to:
sqlServer=myMachine\sql2012
I looked examples online, some of them are just working with set variables, or some are not replacing but inserting. There are some examples are writing into a new file, but the new file has line number in front of each line. I don't find a useful instruction how to write batch scripts, and none of the update file batch scripts works for me.
It will be very helpful if you leave some comments.
Thanks in advance
EDITED - to adapt to comments
#echo off
setlocal enableextensions disabledelayedexpansion
set "config=.\config.txt"
set "dbServer=localhost\sql2012"
for /f "tokens=*" %%l in ('type "%config%"^&cd.^>"%config%"'
) do for /f "tokens=1 delims== " %%a in ("%%~l"
) do if /i "%%~a"=="sqlServer" (
>>"%config%" echo(sqlServer=%dbServer%
) else (
>>"%config%" echo(%%l
)
type "%config%"
endlocal
Input file is read line by line (for /f %%l), then each line is split (for /f %%a) and if the first token in the line is "sqlserver" then the line is replaced, else the original line is sent to file.
The command used to retrieve the information in the first for loop includes an cd.>"%config%" that will remove the contents of the file, so the final resulting lines (that have been read in memory by the for command before removing them) are sent directly to the same file.
You can do this:
FINDSTR /I /V /B "sqlServer=" config.txt > config.new
ECHO sqlServer=myMachine\sql2012 >> config.new
DEL config.txt
REN config.new config.txt
The FINDSTR will remove all lines that start with sqlServer= and create a new file called newfile.
The ECHO will add a line at the end with sqlServer=MyMachine\sql2012.
The last two lines delete your existing config.txt and replace it with the output of the first two lines.
I am trying to achieve some file control with batch processing.
The base idea is a text file is supplied which contains a list of files and folder paths.
What I then need to do is is move these names files to another location referencing the same folder structure. The folder paths also vary some can be 2 deep others up to 4.
The reason for batch is that it can be hundreds of files at a time thus making manual processing time consuming and hard.
An example of the text file input is:
copier\spc240\parts.cat\M095_M096_CHN_V1.05.pdf
copier\spc240\parts.cat\M095_M096_NA_V1.06.pdf
copier\spc240\parts.cat\M099_M100_NA_V1.10.pdf
copier\spc240\parts.cat\M100_CHN_V1.10.pdf
options\df3090\D779_21_V1.01.pdf
options\pb3190\D747_27_V1.00.pdf
Below is what I have managed to do so far:
#echo off
set "file=deletes.txt"
set /A i=0
for /F "usebackq delims=*" %%a in ("%file%") do (
set /A i+=1
call set array[%%i%%]=%%a
call set n=%%i%%
)
for /L %%i in (1,1,%n%) do (
echo|set /p="move %%array[%%i]%% moved\%%array[%%i]%%">>Test.txt
echo.>>Test.txt
)
echo pause>>Test.txt
rename Test.txt RunMe2Move.bat
pause
What this currently does for me is make a batch file that could be run seperately, however this is where I stumble down. This errors as the destination folders do not exist and I am not familiar enough with arrays in batch to split the text lines to enable me to use the mkdir for the missing folders or trim the file names.
In essence I am trying to get the following lines of code either processed or output (using copier\spc240\parts.cat\M095_M096_CHN_V1.05.pdf as example) for each line in the text file.
mkdir moved\copier\spc240\parts.cat\
move copier\spc240\parts.cat\M095_M096_CHN_V1.05.pdf moved\copier\spc240\parts.cat\
Ultimately I am trying to get all of this done in one file if possible, the output is just there so I can check things through.
Can anyone help?
for /f "delims=" %%a in (%file%) do (
SET "fpath=%%~a"
SETLOCAL enableDelayedExpansion
SET "fpath=!fpath:%%~nxa=!"
md "moved\!fpath!"
move "%%~a" "moved\!fpath!"
ENDLOCAL
)
I have a text file with no. of file formats. I want to print those file names to one text file one by one. Problem here is as soon as it check second file format name and try to write in text file, it will erase the first record info.Here is what I have return
#ECHO OFF
setlocal enableDelayedExpansion
SET "formats=.css.js.jsp."
FOR /F "delims=#" %%A in (demo.txt) do (
IF "!formats:%%~xA.=!" neq "!formats!" (
%%~nxA > output.txt
) ELSE (
ECHO Incorrect file format
)
)
Just use >> redirection.
%%~nxA >> output.txt
It will append redirected output to the end of your file, instead of cleaning the file before redirecting output there.