Merging PDF files per the first part of files names - how to automate? [closed] - vba

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I was able to revised a sample VBA code I found here to generate PDF files from an excel file. After this coding runs, the results is that I will have multiple PDF files with suffix "-index.pdf" - (e.g., 875458-indexl.pdf) I can change the suffix to anything.
And I will extract another set of PDF files from a zip file that have the identical file name as 875458.pdf.
At the end, in one folder, there will be multiple pairs of files as below.
875458.pdf
875458-index.pdf
875460.pdf
875460-index.pdf
954784.pdf
954784-index.pdf
My goal is to find a w
ay to merge each pair of files into one file.
The end result would be as below.
(Indexl file being the first pages)
875458.pdf
875460.pdf
954784.pdf
I downloaded PDFtk, but I don't know exactly how script works, and even though I see some samples, I just don't get it...
If someone can kindly shows me the following, I would appreciate it greatly.
1) Which programs I should have installed
2) How to name the batch or bash file, and where to save the file
3) how to trigger the script to run
Thank you.

I've found the command to merge/join two pdf files with PDFtk on this page.
setlocal enabledelayedexpansion
for %%# in (*-index.pdf) do (
set n=%%~n#
set n=!n:~,-6!
pdftk A=!n!.pdf B=%%# cat B A output C:\export\done\!n!.pdf
)
This will get every *-index.pdf and merge it with its respective *.pdf.

I found an example as below. When I execute this, I, at least get the following message. So I think I only need to tweak the script below for correct file names.
Error message when executing this batch file
File Not Found
Done. Input errors, so no output created
Where files are saved and file names
c:\merge
merge.bat (this file contains the code example below)
845499.pdf
845499-index.pdf
845502.pdf
845502-index.pdf
pdftk set up
I installed pdftk pro, and it shows when I do "PATH" in command line, and I also copied pdftk.exe in c:\merge AND c:\windows\system32
code example
#echo off
setlocal EnableDelayedExpansion
rem Initialize (delete) "lastFile" and "fileList" variables
set "lastFile="
set "fileList="
rem Next line get the output of a "dir /B" command, that show file names only
rem "for /F" command execute the dir, get the output and divide each line in two "tokens" ("%%a" and "%%b")
rem with the first part before the "" in "%%a" and the *rest* (including further "") in "%%b"
for /F "tokens=1* delims=_" %%a in ('dir /B _KM_.*') do (
rem If the base file name changed...
if "%%a" neq "!lastFile!" (
rem Process previous file list;
rem this "if" is just to avoid process the empty list the first time
if defined fileList (
pdftk !fileList! output !lastFile!.pdf
)
rem Reinitialize the new list
set "lastFile=%%a"
set "fileList=%%a_%%b"
) else (
rem Append this file to current list
set "fileList=!fileList! %%a_%%b"
)
)
rem Process the last list
pdftk !fileList! output !lastFile!.pdf

Related

How to use command prompt to list file names in directory but exclude 1st 3 characters

I'm trying to write some vba code that sends a line of code to the command prompt and executes it. I have that part down, but I need help getting the actual code to work.
I want to list all of the files in a specific folder that are the .doc file extension, but I want to exclude the first three characters of the filename that gets printed to my output text file. (Note: I'm using vba because this is one of several different commands I'd like to get into a single vba macro, and I cannot use batch files b/c they are blocked on my system so I'd like to work directly with the command prompt)
The following code works and gives me the file names without the file extension (ie. ABC201704.doc will return as ABC201704)
*%comspec% /c for %i in (C:\Test\ABC*.doc) do #echo %~ni >> C:\Test\Output.txt*
However, I don't know how to modify this so that it doesn't include the first 3 characters (ie. I'd like it to return 201704 instead of ABC201704).
Any help would be greatly appreciated! I tried using the following link, but I couldn't figure out how to get that to work for my situation.
Not tested:
#echo off
setlocal enableDelayedExpansion
for %%a in ("C:\Test\ABC*.doc") do (
set docname=%%~nxa
echo !docname:~3!
)
In command prompt:
cmd /v:on /c "for %a in ("C:\Test\ABC*.doc") do set docname=%~nxa & echo !docname:~3!"

How do I use command prompt to identify derivative generation drop off?

I have one folder of approximately 7500 pdfs and a second folder with approximately 7300 tiff derivatives. Somewhere over the past 4 days of processing, intermittent tiff derivative generation failure occurred. How do I identify which files dropped off?
So far Ive tried:
diff -rq folder_pdfs folder_tiffs
However that reports all files as different given the difference in file extensions.
How do I identify which files dropped off?
Use the following batch file.
MissingFiles.cmd:
#echo off
setlocal
for /f %%f in ('dir /b folder_pdfs') do (
if not exist folder_tiffs\%%~nf.tiff (
echo folder_tiffs\%%~nf.tiff
)
)>>MissingFiles.txt
endlocal
Notes:
MissingFiles.txt will contain the list of missing files.
Example:
F:\test>dir /b folder_pdfs
1.pdf
2.pdf
3.pdf
4.pdf
5.pdf
F:\test>dir /b folder_tiffs
1.tiff
3.tiff
5.tiff
F:\test>MissingFiles.cmd
F:\test>type MissingFiles.txt
folder_tiffs\2.tiff
folder_tiffs\4.tiff
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
dir - Display a list of files and subfolders.
for /f - Loop command against the results of another command.
parameters - A command line argument (or parameter) is any value passed into a batch script.

Renaming files using the parent folder/directory name as a variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to create a batch script that will rename all the documents in all sub-folder with the parent folder name and then the original file name. For example:
There are 200 folders with unique 4 digit names, all the folders contain a folder called "Data Sheets". I would like to rename all the documents in the Data Sheets folder to include the 4 digit unique id name. So essentially I want the documents to be renamed from data sheet.doc to 1234 data sheet.doc
There are many batch scripts out there that do something very close to what I am trying to do but I have been unable to modify them to do exactly what I need. Any help would be appreciated. This is what I have been trying of similar versions trying to get it to work.
#echo off
for /r %%a in ("S:\Fiscal Yr. 2015\*\*.doc") do (
for /d %%d in (%CD%) do (
set newname=%%~nd%~x1
ren "%%~a" "!newname!%%~Xa"
echo media file in %%~fa renamed to "!newname!%%~Xa"
)
)
Zach
To clarify - this will process every .doc file in any folder called "Data Sheets" and rename the files to add the parent folder-name to "Data Sheets" to the start of the .doc files.
This will echo the rename command for you to confirm that it is correct, before removing the first echo statement.
#echo off
for /r "S:\Fiscal Yr. 2015" %%a in (*.doc) do (
for /f "delims=" %%b in ("%%~pa\.") do (
if /i "%%~nxb"=="Data Sheets" for /f "delims=" %%c in ("%%~pb\.") do (
echo ren "%%a" "%%~nxc %%~nxa"
echo media file "%%a" renamed to "%%~nxc %%~nxa"
)
)
)
pause
Here's a simple example of how you can rename all files in an entire folder structure, including all sub-folders:
#echo off
setlocal ENABLEDELAYEDEXPANSION
cd /d "S:\Fiscal Yr. 2015"
set _cd=%CD%
for /f "delims=" %%i in ('dir *.doc /a-d /s /b') do (
set old_fn=%%~fni
set new_fn=!old_fn:%_cd%\=!
set new_fn=!new_fn:Data Sheets\=!
set new_fn=!new_fn:\= !
echo ren "!old_fn!" "!new_fn!"
)
endlocal
The for command enumerates all the .doc files in in the current folder and sub-folders (\s switch). The \b switch makes it output the filenames using bare format (which in this case includes the full path). The /a-d makes it ignore directories, so only files get renamed.
Using setlocal / endlocal makes it possible to use delayed expansion of variables, and thus we use ! instead of % with the set commands. Delayed expansion ensures that the variables are reevaluated when they are used, instead of using the original value assigned to them.
The echo ren ... makes it echo the rename commands to the console instead of actually performing the renames. You'll want to verify it outputs the right rename statements before you run it for real, and it would be a good idea to backup the folder structure before you do it for real since there is no undo for this type of operation.
The batch file removes the leading path and only keeps the sub-folder's name, adding it before the filename, also removing the Data Sheets folder name from the new filename.

What languages are recommended to insert output from windows cli into a sqlite table? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I would like to recursively go through all files ending in .mp3 or .flac (case-insentive) in my C:\Songs directory and pass each file path as an argument to a binary file run from windows command prompt which returns a string representation of the audio fingerprint.
I then want to insert a record into a 2xn sqlite table, where n is the number of songs I have. The two fields are the filepath and the output of the command run from the windows command prompt.
I need some help getting started. I can >> the command line output to a flat file. I can also and insert a record into a sqlite database. However I don't know what languages I can use to pass the output from the command line directly to the sqlite table. Do you have any recommendations of languages and how to use them to achieve the desired goal?
Also - I'm using Windows 7 but I assume the routine would be same if I was using another windows os.
Using a batch script
#echo off
setlocal
pushd "C:\Songs"
REM Get the file count
for /f %%N in ('dir /b /a-d *.mp3 *.flac^|find /c /v ""') do set "cnt=%%N"
REM The file count is now in the cnt variable. I'll simply ECHO the value
echo File count = %cnt%
REM Process each file
for %%F in (*.mp3 *.flac) do (
REM The file name is now in %%F variable
REM Run your command - I'm assuming it only produces 1 line of output
for /f "delims=" %%A in ('yourCommand yourArgs "%%F"') do (
REM the command output is now in %%A
REM Write your records. You will have to come up with the syntax here
REM I'll simply echo the record to the screen
echo "%%F", "%%A"
)
)
popd
You could use a Windows batch script for this. #dbenham's answer looks like a good start if that's the approach you want to take (and I upvoted that answer).
But if you want real error checking, or you want to enhance this over time I'd recommend a real programming or scripting language. There are a lot of good choices. There are ways to use JavaScript or PHP.
Personally I write this type of code in Python and have been very happy with it. You can launch a command, read it's stdout, etc. Plus it has support for sqlite built right in.

Creating a bat file which executes SQL scripts

I have a folder into which a number of MSQL scripts get dropped into after each weekly sprint. For example, 10 scripts were placed into the folder today. I had to then open each script individually and run it against the applicable database. The database that it needs to be run against is in the name of the file.
e.g. [2] [CRMdata]UpdateProc.sql
The [2] represents the sequence in which it is run, so script [1] needs to be run before it.
[CRMdata] is the database I have to run it against.
This process is very tiresome, especially if there are 50 scripts to run sequentially.
I was wondering if there was an easier way to do this?
Perhpas a .bat file, which reads the filename, and executes the scripts sequentially based on the script number, as well as executing it against the database specified in the file name.
Any help is much appreciated.
Thanks.
First, when you need to run things, consider using SQL Server Job Agent. This is a good way to schedule simple things.
For a task like this, I would recommend PowerShell in combination with "sqlcmd". This command is actually the answer to your question, since it will run scripts from the command line.
However, go a step further. Schedule a job that runs once per week (or whenever you want it run). Have it consist of one step, a PowerShell script. This can then loop through all the scripts in the directory, extract the file name from the name, and run the script using sqlcmd. Along the way, also log what you are doing in a table so you can spot errors.
I don't know anything about executing SQL with MSQL. You will have to work out how to run each script against the proper database using whatever command-line utility is provided for MSQL.
I can help you with a batch file that will sort the SQL files in the correct sequence order, and parse out the name of the database.
The job is much easier in batch if the sequence numbers are zero prefixed to be a constant width. I'm assuming it is OK to rename the files, so that is what this solution does.
I also assumed you will never have more than 999 files to process. The code can easily be modified to handle more.
Some changes will have to be made if any file names contain the ! character because delayed expansion will corrupt the expansion of the FOR variables. But that is an unlikely problem.
#echo off
setlocal enableDelayedExpansion
:: Change the definition to point to the folder that contains the scripts
set "folder=sqlCodeFolder"
:: The mask will only match the pattern that you indicated in your question
set "mask=[*] [*]*.sql"
:: Rename the .sql files so that the sequence numbers are zero prefixed
:: to width of 3. This enables the default alpha sort of the directory to be
:: in the proper sequence
for /f "tokens=1* delims=[]" %%A in ('dir /b "%folder%\%mask%"') do (
set seq=00%%A
ren "%folder%\[%%A]%%B" "[!seq:~-3!]%%B"
)
::Process the renamed files in order
for %%F in ("%folder%\%mask%") do (
for /f "tokens=2 delims=[] " %%D in ("%%~nF") do (
rem %%F contains the full path to the sql file
rem %%D contains the name of the database, without enclosing []
rem Replace the echo line below with the proper command to run your script
echo run %%F against database [%%D]
)
)