I'm trying to merge multiple PDF's calling pdftk from a batch or VBS script file.
The issue is that the PDF's are all located in subfolders within a common parent directory. Example: parent folder > subfolder1, subfolder2, subfolder3, where the script/BAT file is located in parent folder and each subfolder contains a PDF.
pdftk is called as follows: pdftk.exe *.pdf cat output OutputFile.pdf
This will merge only the PDF's in the current directory though, and not in the subfolders.
Is there a way to do one of the following:
Use the VBS/BAT file to copy all PDF's in the subfolders to another directory, and not error-out if some of the subfolders are empty
Have pdftk look through the subfolders to select the PDF's to merge
Any help would be appreciated.
Try this:
Wscript.Echo "begin."
Dim pdffiles
pdffiles=""
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSuperFolder = objFSO.GetFolder(WScript.Arguments(0))
Call ShowSubfolders (objSuperFolder)
Dim objShell
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "pdftk.exe", pdffiles & " output combined.pdf", "", "runas", 1
Wscript.Echo "end."
WScript.Quit 0
Sub ShowSubFolders(fFolder)
Set objFolder = objFSO.GetFolder(fFolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If UCase(objFSO.GetExtensionName(objFile.name)) = "PDF" Then
pdffiles=pdffiles & " " & objFile.Name
End If
Next
For Each Subfolder in fFolder.SubFolders
ShowSubFolders(Subfolder)
Next
End Sub
Related
Update-----
My vbs script should take camera photos and rename them from a unique name like "0634_IMG" to a recursive number from 01 to 100. For example say there are 3 photos in the folder: 001_IMG, 003_IMG, and 006_IMG my script should rename these files to 01, 02, and 03 respectively.
I have a version that works when I drag and drop the script into the specific folder, but there are 1000's of folders so I want to be able to place it into the parent folder and it execute on all subfolders.
So it should be a folder drill down that only looks for files with the extension GIF, IMG, and PNG.
Folder Structure: Location>Block#>Letter(comprised of 3 folders A, B, and C)>Chamber(for each letter there are 4 subfolders)>Pictures (each subfolder has the pictures I am trying to rename)
so to review, I want to be able to put the script in the same folder as the block# and it execute on the pictures in the last folder for every subfolder. So after I run the script each picture should be renamed 01-100 and maintain its position within the folder scheme.
Thanks to the help of CHNguyen, my code was edited so that it would maintain the folder structure I describe above.
The issue now is that the script is numbering the pictures in every folder continuously and does not start or restart at 1.... For example after executing the script, Folder 1 (which contains 30 images) is outputting file names 830-860, when it should be 1-30. Additionally, the other subfolders have this same issue and it seems that the count or "intFileParts" is not being reset and I can't get it to reset.
I ask the coding gods for help as I am a newb and thanks in advance.
Option Explicit
Dim fso
Dim oFolder, oSubFolder
Dim oFile
Dim sPath, strOldName, strNewName
Dim intFileParts
' Create the instance of the fso.
Set fso = CreateObject("Scripting.FileSystemObject")
' Set the folder you want to search.
sPath = fso.GetFolder(fso.GetAbsolutePathName(".")) + "\"
RenameFiles(sPath)
Sub RenameFiles(Path)
Set oFolder = fso.GetFolder(Path)
intFileParts = 1 ' Restart at 1
' Loop through each file in the folder.
For Each oFile In oFolder.Files
' Only select images
Select Case oFile.Type
Case "GIF Image", "JPG Image", "PNG Image"
End Select
' Get complete file name with path.
strOldName = oFile.Path
' Build the new file name.
strNewName = ""
strNewName = fso.GetParentFolderName(oFile) & "\" & Right("000" & fso.GetBaseName(oFile), 3) & "." & fso.GetExtensionName(oFile)
' Use the MoveFile method to rename the file.
fso.MoveFile strOldName, strNewName
intFileParts = intFileParts + 1
Next
For Each oSubFolder In oFolder.Subfolders
RenameFiles(oSubFolder.Path)
Next
End Sub
Set oFile = Nothing
Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing
This should do:
I reworked the ' Build the new file name. section to properly get the file's parent folder using fso.GetParentFolderName() to "maintain its position within the folder scheme". The padding and incrementing of the numeric value in the filename was also improved/simplified using VB and fso methods.
The "missing" code under ' Use the MoveFile method to rename the file. was also added to perform the rename via fso.MoveFile()
Code:
Option Explicit
Dim fso
Dim oFolder, oSubFolder
Dim oFile
Dim sPath, strOldName, strNewName
Dim intFileParts
' Create the instance of the fso.
Set fso = CreateObject("Scripting.FileSystemObject")
' Set the folder you want to search.
sPath = fso.GetFolder(fso.GetAbsolutePathName(".")) + "\"
RenameFiles(sPath)
Sub RenameFiles(Path)
Set oFolder = fso.GetFolder(Path)
intFileParts = 1 ' Restart at 1
' Loop through each file in the folder.
For Each oFile In oFolder.Files
' Only select images
Select Case oFile.Type
Case "GIF Image", "JPG Image", "PNG Image"
End Select
' Get complete file name with path.
strOldName = oFile.Path
' Build the new file name.
strNewName = ""
strNewName = fso.GetParentFolderName(oFile) & "\" & Right("000" & intFileParts, 3) & "." & fso.GetExtensionName(oFile)
' Use the MoveFile method to rename the file.
fso.MoveFile(strOldName, strNewName)
intFileParts = intFileParts + 1
Next
For Each oSubFolder In oFolder.Subfolders
RenameFiles(oSubFolder.Path)
Next
End Sub
Set oFile = Nothing
Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing
I have a number of folders, each with files inside the folder.
The structure looks something like this:
Folder.No.1
Folder_No_2
Folder No 3
and the files within are something like:
Folder.No.1\My.Movie.1.mp4
Folder.No.1\My.Movie.1.txt
Folder_No_2\My_Movie_2.mp4
Folder_No_2\My_Movie_2.jpg
Folder_No_2\My_Movie_2.txt
Folder No 3\My Movie 3.mp4
As you can see, some folders contain . in the name, some contain _ and some contain spaces.
The one consistent factor is that each folder will always contain an .mp4 file, regardless of anything else.
Therefore, how can I change the Date Modified date/time of the folder to match that of the .avi file contained within the folder? Can I do this by copying the DateLastModified from the file inside (the child) to the parent folder using VBScript?
So far I am working on something like this:
Dim objShell, objFolder, objFile
Set objFile = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(strDir)
Set strDir = objFile.GetFolder("C:\Temp")
For Each objFile In objFolder
If UCase(objFolder.GetExtensionName(objFile.Name)) = "MP4" Then
objFolder.Items.Item(strDir).ModifyDate = DateLastModified
WScript.Echo objFolder.Name
End If
Next
but it fails when calling from command line with: cscript CopyDateToParent.vbs
Can anyone please help to correct this to make it work?
Try my code :
StrFolder="C:\Users\admin\Desktop\" 'Your folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objFSO.GetFolder(StrFolder)
Set Folder = objShell.NameSpace(StrFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Name=objFile.Name
strExtension = LCase(objFSO.GetExtensionName(Name))
If strExtension = "mp4" Then
Set objFolderItem = Folder.ParseName(Name)
objFolderItem.ModifyDate = DateLastModified 'Example : "01/01/2008 8:00:00 AM"
End If
Next
I've looked around and all examples I can find are very general purpose, I simply need code to delete a single .json file from a randomly named subfolder within the Temporary Internet Files folder.
Currently I am downloading a very small file each time my VBScript is ran, however it seems to download that file to Temp Internet Files and on each subsequent run, grabs it from there instead of the Internet. This is a file that must always be new.
How can I search through all sub-folders within Temp Internet Files and delete forge[1].json? AND, delete it's parent folder?
For dbmitch:
' DELETE CACHED FORGE FILE ------------------------ '
Function DelFiles()
look_subfolders UserProfile & "AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\"
Set Folder = objFso.GetFolder ( fold )
Sub look_subfolders ( fold )
For Each objFile in Folder.Files
If objFile.Name = "forge[1].json" Then
objFile.Delete
End If
Next
'look into subfolders:
For Each Subfolder in Folder.SubFolders
look_subfolders Subfolder.Path
Next
End Sub
End Function
Set objFso = CreateObject( "Scripting.FileSystemObject" ) is defined elsewhere at the top of the script (which has worked for every other object I've used).
I also tried this too...
Path = UserProfile & "AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5"
Set objFolder = objFso.GetFolder(Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If objFile.Name = "forge[1].json" Then
objFile.Delete
End If
Next
For Each objFile In objFolder.SubFolders
If objFile.Name = "forge[1].json" Then
objFile.Delete
End if
Next
It deletes the file from the main folder, but doesn't delve into any subfolders.
Try this, it's a recursive call:
Set fs = CreateObject("Scripting.FileSystemObject")
look_subfolders "C:\windows\temp" 'change the path to your temp folder
Sub look_subfolders(fold)
Set Folder = fs.GetFolder(fold)
'folder files:
For Each objFile in Folder.Files
If objFile.Name = "forge[1].json" Then
objFile.Delete
msgbox "File found and deleted" 'message to confirm deletion
WScript Quit
End If
Next
'look into subfolders:
For Each Subfolder in Folder.SubFolders
look_subfolders Subfolder.Path
Next
End Sub
Basically I'm trying to unzip some specific files in a zip file (there are lots of junk subfolders in it).
The thing is only the last subfolder contains files I want. Other subfolders won't contain any files except another subfolder.
Here is the code I'm currently using:
ZipFile="C:\Test.zip"
ExtractTo="C:\"
Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(ExtractTo) Then
fso.CreateFolder(ExtractTo)
End If
set objShell = CreateObject("Shell.Application")
set FilesInZip= objShell.NameSpace(ZipFile).items
print "There are " & FilesInZip.Count & " files"
'Output will be 1 because there is only one subfolder there.
objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)
Set fso = Nothing
Set objShell = Nothing
Is there anyway I can traverse subfolder and only unzip files with a specific extension?
You can do that with a recursive procedure that calls itself for folder items and extracts file items if they have a specific extension:
Set fso = CreateObject("Scripting.FileSystemObject")
Set app = CreateObject("Shell.Application")
Sub ExtractByExtension(fldr, ext, dst)
For Each f In fldr.Items
If f.Type = "File folder" Then
ExtractByExtension f.GetFolder, ext, dst
ElseIf LCase(fso.GetExtensionName(f.Name)) = LCase(ext) Then
app.NameSpace(dst).CopyHere f.Path
End If
Next
End Sub
ExtractByExtension app.NameSpace("C:\path\to\your.zip"), "txt", "C:\output"
This is my first post so please excuse my ignorance. I am using a vbscript to zip all .csv type files in a particular folder. After some google searches, I have found a workable vbscript to do this and have enabled a scheduled task to automate this.
What I need to do next is to transfer the zip file via sftp and then "move" that zip file into another folder. I believe the former can be achieved using pscp.exe via command line but can someone show me how to do the latter?
Basically the zipping will be done twice a day and so it will have a timestamp similar to yyyymmdd0900.zip (for 9am schedule) and yyyymmdd1800.zip (for 6pm schedule). After the transfer, I want to move (not copy) the zip file generated into another folder.
Any pointers would be greatly appreciated. Thank you all in advance.
EDIT: Here is some code I slapped together based on some Google searches. It does what I want it to do. Please excuse the "pasting" as i couldn't figure out how to format it properly. Currently, it runs the bat file after copying but I just noticed that i need to send (using PuTTY Secure Copy) the "latest" zip file before moving it to the "completed" folder. Can someone please show me how to do this?
Zipping the file and rename the zip file
My Code :
On Error Resume Next
strFilepath = "c:\files"
strDestination = "c:\files\completed\"
strExtension = "csv"
strYear = Year(Now)
strMonth = Right("0" & Month(Now), 2)
strDay = Right("0" & Day(Now), 2)
strHour = Right ("0" & Hour(Now), 2)
strMinute = Right ("0" & Minute (Now), 2)
strZip = strFilepath & "\" & strYear & strMonth & strDay & strHour & strMinute & ".zip"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFilepath)
For Each objFile in objFolder.Files
strFileExt = objFSO.GetExtensionName(objFile.Path)
If LCase(strFileExt) = LCase(strExtension) Then
ZipFile objFile.Path, strZip
End If
Next
Sub ZipFile(strFileToZip, strArchive)
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists(strArchive) Then
Set objTxt = objFSO.CreateTextFile(strArchive)
objTxt.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
objTxt.Close
End If
Set objApp = CreateObject( "Shell.Application" )
intCount = objApp.NameSpace(strArchive).Items.Count + 1
objApp.NameSpace(strArchive).CopyHere strFileToZip
Do
WScript.Sleep 200
set objNameSpace = objApp.NameSpace(strArchive)
If Not objNameSpace is nothing Then
If objNameSpace.Items.Count = intCount Then
Exit Do
End If
End If
Loop
End Sub
>Move file to a different folder and then run a bat file to secury copy file to a FTP location
'Vars
Dim objFSO, objFileCopy, objFileDelete, dot, files, file
Dim strDestination, folder, subfolder, fileCount, strFilePath
'Strings
strDestination = "C:\Files\Completed\"
strFilePath = "C:\Files"
set objFSO = CreateObject("Scripting.fileSystemObject")
set folder = objFSO.getFolder(strFilePath)
For Each file In folder.files
Set objFileCopy = objFSO.GetFile(file)
If objFSO.GetExtensionName(file) = "zip" Then
objFSO.MoveFile objFileCopy.Path, strDestination
End If
Next
Dim shell
Set shell=createobject("wscript.shell")
Shell.run "C:\testsend.bat"
Set shell=nothing
This will move a file to the specified location.
Sub Move_File(Source_File, Destination_Folder)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.MoveFile Source_File, Destination_Folder
Set fso = Nothing
End Sub
sftp client provides a means to change working directory on the host before performing any file transfers. It would be better to thus transfer the file directly to the target location.
NOTE: The above answer was a result of misunderstanding the question. I read it to mean the file had to be moved on the destination but the real operation was to move the file on the origin.
I found the following example code that moves a file after checking that it exists. Wildcards are allowed for the source parameter but then FileExists may not work. Requires vbscript 2.0 to work.
<%
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("c:\sourcefolder\anyfile.html") Then
filesys.MoveFile "c:\sourcefolder\anyfile.html", "c:\destfolder\"
End If
%>