Almost working PDF merge batch file, just a little help needed - pdf

I'm using this batch file to merge PDF files, and it's working, but only with files which don't have spaces in their names. For a 'test file.pdf' it gives an error ~ can't find file test and file.pdf. With what should I replace the "*.PDF" to get it to work correctly?
#echo on
setlocal enabledelayedexpansion
FOR %%A IN ("*.PDF") DO (set command=!command! %%A)
%~dp0\pdftk.exe %command% cat output "%~dp1merged_PDF.pdf"

Replace set command=!command! %%A with set "command=!command! "%%~A"".
For naming the merged file after its parent folder you can determine the parent folder name like this:
for %%a in ("%~dp1.") do set "name=%%~nxa"
and then create the output file like this:
%~dp0\pdftk.exe %command% cat output "%~dp1%name%.pdf"

Related

Batch Windows - How to create a loop to rename many files in different folders?

I have PDF files in one location (all in the same folder), which I need to take 3 useful information from the file name.
And I have .jpg files in another location (1 picture per folder) which I need to rename with these information taken from the PDF.
My script is able to find the information, store and rename but it only works for the first file in a directory and then stops.
I need to make it run in a loop until there is either no more PDF files to take information from OR no more .jpg files to be renamed.
Can someone help me to make this script run in a loop?
echo off
setLocal EnableDelayedExpansion
rem User input
SET /P datework= Please type the date you want to work (format yyyymmdd):
rem Folder where the PDFs are located - extract the useful information from file name
cd /D C:\Users\A\Desktop\A_tests\QC\PDF\%datework%\
for %%i in (*.pdf) do (
set RcvLn=%%i
set RcvLn=!RcvLn:~0,4!
set GunStn=%%i
set GunStn=!GunStn:~5,4!
set Node=%%i
set Node=!Node:~10,4!
)
rem Rename the pictures using the values stored on the variables
xcopy /Y "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\%datework%\Node %Node%\*.jpg" "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\%datework%\Node%Node%_RL%RcvLn%_GS%GunStn%.jpg"
You set each variable inside of the loop for each file, but then you do the xcopy outside of the loop which will only do the xcopy once. So we rather do the xcopy inside the loop.
echo off
setLocal EnableDelayedExpansion
rem User input
SET /P datework= Please type the date you want to work (format yyyymmdd):
rem Folder where the PDFs are located - extract the useful information from file name
cd /D C:\Users\A\Desktop\A_tests\QC\PDF\%datework%
for %%i in (*.pdf) do (
set RcvLn=%%i
set RcvLn=!RcvLn:~0,4!
set GunStn=%%i
set GunStn=!GunStn:~5,4!
set Node=%%i
set Node=!Node:~10,4!
echo xcopy /Y "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\!datework!\Node !Node!\*.jpg" "C:\Users\A\Desktop\A_tests\QC\UHD73\Node Deployment\!datework!\Node!Node!_RL!RcvLn!_GS!GunStn!.jpg"
)

Remove section of filename

I am needing a batch file to remove a certain part from multiple filenames in the same directory.
Example:
I have over 80,000+ files with the title like so:
Test Title, The - Conspiracy.zip
I am needing ", The" removed from file names leavin the titles like so:
Test Title - Conspiracy.zip
PS, I am needing this in Batch file only!
Any help is much appreciated!
THANX!!!
I found what I needed to use and thank you all for the quick replies and help!
#echo off &setlocal
set currentDirectory="%CD%"
for /f "delims=" %%a in ('dir /b /a-d *, The*.*') do (
set "fname=%%~a"
setlocal enabledelayedexpansion
set "nname=!fname:, The=!"
ren "!fname!" "!nname!"
endlocal
)
If you can use a Unix command shell, you could use the mv command in a loop.
You could download cygwin or Git Bash, or if you have Windows 10, you could do this right in the command line (assuming you've updated):
Creating a file like this
#!/bin/bash
for file in *.zip
do
removedPart=", The"
mv "${file}" "${file/removedPart/}"
done
You might want to test the command on a single file first to be sure it does what you want. i.e.
file=Test Title, The - Conspiracy.zip
removedPart=", The"
mv "${file}" "${file/removedPart/}"
You can loop through the contents of the file directory in something like this loop. Batch script loop
Then when you're looping through you can replace the contents of the file name. Look at this: String replacement in batch file
Sorry not more specific as Batch scripting isn't my thing. But this logic should prove to at least be helpful. Someone my post something better.

Set variables for each line of a txt file in a batch code

Is there a way to set each line of a txt document as a different variable in a batch file? I have got a txt file with all drives found on the computer. How can I set each line (for each drive) as a different variable, if this is possible at all?
I have a file which writes all avalible drives in a text file. I want to read this file out and set a different variable for each line of text. For example:
DeviceID
C:
D:
E:
Now "DeviceID" would be the first variable. "C:" "D:" and "E:" are all getting individual variables. I hope you get what I mean.
Thanks in advance!
Okay so I've tried this:
#echo off
setlocal enabledelayedexpansion
set i=0
for /f "tokens=2 delims=:=" %%a in ('wmic logicaldisk get name /value ') do (
set /a i+=1
set drive[!i!]=%%a
)
set drive
pause
copy %0 %drive[1]%:\%random%.bat
pause
And it works pretty well. But I want to copy the currently executed file to all drives found without typing:
copy %0 %drive[1]%:\currentlyexecutedfile.bat
copy %0 %drive[2]%:\currentlyexecutedfile.bat
copy %0 %drive[3]%:\currentlyexecutedfile.bat
copy %0 %drive[4]%:\currentlyexecutedfile.bat
Is there a way to do this, without knowing how many drives there are and without causing errors?
Is there a way to do this, without knowing how many drives there are
and without causing errors ?
You should do it like this :
#echo off
setlocal enabledelayedexpansion
set i=0
for /f "tokens=2 delims=:=" %%a in ('wmic logicaldisk get name /value ') do (
set /a i+=1
set drive[!i!]=%%a
)
Rem set drive
Rem If you don't know how many elements the array have (that seems is the case), you may use this method:
for /F "tokens=2 delims==" %%s in ('set drive') do (echo Copy %0 "%%s:\currentlyexecutedfile.bat")
pause

Set file name with * as variable batch script

I have a batch file that i'm having issues with. I need to find the name of a file, then set it to a variable. Then I will use this to pass it onto a vbs script to further look into the file. The name of the file is logfile_date_time.log but the time varies depending on what time is starts. The point of the batch file is to find out the last modified date of this file.
set fordate=%date:~4,2%%date:~7,2%%date:~10,4%
set filename=c:\logfile_%fordate%_*.log
if exist %filename% (goto exist) else (goto noexist)
:exist
vbsscript.vbs /file:%filename%
goto end
:noexist
file doesn't exist code blah blah
:end
pause
I had to modify the names of folders and remove some code for security purposes since this is for work.
Any help appreciated. Thanks!
not tested:
set "last_modified="
for /f "delims=" %%f in ('dir /a-d /tw /o-d /b^| findstr /r /i /c:"logfile_[0-9][0-9]*_.log"') do (
do set "last_modified=%%~dpfnxf"
goto :break_loop
)
:break_loop
if defined last_modified echo file %last_modified% exist ...
The problem with your code is that it doesn't expand the wildcard character (*), and your VBScript probably doesn't handle wildcards in filenames by itself (the FileSystemObject methods for instance don't). If the file you want to process is the only one matching your pattern, you could do something like this:
#echo off
setlocal
set "fordate=%date:~4,2%%date:~7,2%%date:~10,4%"
pushd C:\
for %%f in (logfile_%fordate%_*.log) do vbsscript.vbs /file:"%%~ff"
popd

How do I use a for loop to get file names and then use them?

I have a folder with the files a.txt, b.txt, and c.txt in it. and I want to use a for-loop to get the names of these files, store them in a variable, and then add them to another text file. The text files are located in the same file as my bat file. Is it possible to do this? If so please show me, these for loops confuse the crap out of me...
What i have now
#echo on
SET name=hey
echo >text.txt
for %F in (*.*) do (set name=#fname
echo name >> text.txt)
#Echo OFF
(For %%# in ("*.txt") do (
Set "FileName=%%~n#"
Call Echo %%FILENAME%%
))>"MyFilenames.txt"
Pause&Exit
NOTE 1: Files are stored in "Filename" var, but is not really necessary, you can directly write the filename to the textfile.
NOTE 2:If you want a recursive loop through files use the /R switch of For command.
NOTE 3:If you want also the file extension change this: "%%~n#" to this else: "%%~nx#"
**UPDATE:**
An alternative script:
#Echo OFF
For %%# in (*.txt) do (Echo %%~n#>> "MyFiles.txt")
Pause&Exit