| Notices | Welcome to the Altnation forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. | | Tech Chat Computers, mobile phones, mp3 players, file all "electronic gadgets" in here. |  | |
21st February 2008, 10:32am
|
#1 | | Punisher Moderator
Join Date: Sep 2002 Location: Glasgow
Posts: 9,500
| Script to print all files in folder and subfolders Can you write a piece of code or a script to print all the files in a folder and its sub folders? I can write a vbs script to print all files in one folder but I have no idea how to include sub folders.
The script I've been using is shown below.
Any help?
TargetFolder = "EXAMPLE FOLDER"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set colItems = objFolder.Items
For Each objItem in colItems
objItem.InvokeVerbEx("Print")
Next
__________________ No matter where you go, you are what you are player
And you can try to change but that's just the top layer
Man, you was who you was 'fore you got here http://allannolove.wordpress.com |
| |
21st February 2008, 10:40am
|
#2 | | Forever Waiting.
Join Date: Nov 2004 Location: Glasgow
Posts: 4,183
| Re: Script to print all files in folder and subfolders This sounds like what you need: (The second part) http://www.microsoft.com/technet/scr...4/hey1020.mspx
The basic idea is that you recursively loop through each of the folders in the top folder and print the files in there. |
| |
21st February 2008, 11:23am
|
#3 | | Punisher Moderator
Join Date: Sep 2002 Location: Glasgow
Posts: 9,500
| Re: Script to print all files in folder and subfolders I've had a play with that but for some reason it hates the line
Set colItems = objFolder.Items
When I change it to
Set colItems = objFolder.files
It will display the name of each file on the screen but not print them.
Here's my current script Quote:
Set objFSO = CreateObject("Scripting.FileSystemObject")
TargetFolder = "I ingaporeCaley CDR drawings and slidesCDR PACKAGE- 13-2-08IROV"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set colItems = objFolder.Items
For Each objItem in colItems
objItem.InvokeVerbEx("Print")
Next
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set objFolder = objFSO.GetFolder(TargetFolder)
Wscript.Echo objFolder.Path
Set colItems = objFolder.items
For Each objItem in colItems
objItem.InvokeVerbEx("Print")
Next
Wscript.Echo
ShowSubfolders objFSO.GetFolder(objFolder)
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.items
For Each objItem in colItems
objItem.InvokeVerbEx("Print")
Next
Wscript.Echo
ShowSubFolders Subfolder
Next
End Sub
|
__________________ No matter where you go, you are what you are player
And you can try to change but that's just the top layer
Man, you was who you was 'fore you got here http://allannolove.wordpress.com |
| |
21st February 2008, 1:48pm
|
#4 | | MANLEGEND SuperMod
Join Date: Sep 2002 Location: Las Vegas, NV
Posts: 27,132
| Re: Script to print all files in folder and subfolders with WMI/VBS it's a pain in the ass (but since I'm dead at work, I might as well give it a shot when I go to work today)
If you just need it enumerated and VBS isn't a requirement, how about
Start -> Run -> CMD
dir /s NAMEOFDIRECTORY > c:/directory.txt
Open c:/directory.txt in notepad or whatever. Hit PRINT.
I'd use
dir /s /b NAME > c:/directory.txt
since it'll just spit back filenames |
| |
21st February 2008, 2:01pm
|
#5 | | MANLEGEND SuperMod
Join Date: Sep 2002 Location: Las Vegas, NV
Posts: 27,132
| Re: Script to print all files in folder and subfolders Yeah...you were going about it the hard way Code: const ForAppending = 8
textFilePath = "C:test.txt"
set objFSO = createobject("Scripting.FileSystemObject")
set objTextFile = objFSO.opentextfile(textFilePath,ForAppending)
strFolder = "C:ringtones"
set objFSO = createobject("Scripting.FileSystemObject")
GetFiles strFolder
sub GetFiles(byval strDirectory)
set objFolder = objFSO.GetFolder(strDirectory)
for each objFile in objFolder.Files
objTextFile.WriteLine objFile.Path
next
for each objFolder in objFolder.SubFolders
GetFiles objFolder.Path
next
end sub
objTextFile.Close Things you could add - check that the text file to be written to exists, create if not. Error checking, file attributes, pass in text file or target folder as arguments..that sorta thing.
Unless you want to go about this in a totally tarded method of sending things to a printer? |
| |
21st February 2008, 2:05pm
|
#6 | | Punisher Moderator
Join Date: Sep 2002 Location: Glasgow
Posts: 9,500
| Re: Script to print all files in folder and subfolders Nah i don't want to print a list of all files in a folder and sub folder, I want to print all the files (.dwg AutoCAD files).
Or am I getting what's written in that code all wrong?
__________________ No matter where you go, you are what you are player
And you can try to change but that's just the top layer
Man, you was who you was 'fore you got here http://allannolove.wordpress.com |
| |
21st February 2008, 2:10pm
|
#7 | | MANLEGEND SuperMod
Join Date: Sep 2002 Location: Las Vegas, NV
Posts: 27,132
| Re: Script to print all files in folder and subfolders My mistake - looked at code more than the question (and all you kept doing was enumerating the files)
VBS isn't the right tool to print out a bunch of CAD files. About the best it can do is open the file and pop open the Print dialog box in my experience
Which CAD application using?
And a quick run around the google shows automated autocad printing generally requiring a program add-on..if I were more familiar with CAD terminology I might be able to drill down on a search to pull out something useful.
Last edited by PapaZeb; 21st February 2008 at 2:10pm.
Reason: Automerged Doublepost
|
| |
21st February 2008, 2:40pm
|
#8 | | Punisher Moderator
Join Date: Sep 2002 Location: Glasgow
Posts: 9,500
| Re: Script to print all files in folder and subfolders I'll do it the hard way.
Cheers though.
__________________ No matter where you go, you are what you are player
And you can try to change but that's just the top layer
Man, you was who you was 'fore you got here http://allannolove.wordpress.com |
| |
21st February 2008, 2:46pm
|
#9 | | MANLEGEND SuperMod
Join Date: Sep 2002 Location: Las Vegas, NV
Posts: 27,132
| Re: Script to print all files in folder and subfolders Actually....if the first script you put up will print the CAD files in a single folder just fine, try (change the strFolder= line, obviously)
set objFSO = createobject("Scripting.FileSystemObject")
strFolder = "C:\ringtones"
GetFiles strFolder
sub GetFiles(byval strDirectory)
set objFolder = objFSO.GetFolder(strDirectory)
for each objFile in objFolder.Files
objItem.InvokeVerbEx("Print")
next
for each objFolder in objFolder.SubFolders
GetFiles objFolder.Path
next
end sub
Last edited by PapaZeb; 21st February 2008 at 2:48pm.
Reason: Removed CODE tags, kept stripping backslashes
|
| |
21st February 2008, 2:54pm
|
#10 | | Punisher Moderator
Join Date: Sep 2002 Location: Glasgow
Posts: 9,500
| Re: Script to print all files in folder and subfolders It does yeah. Cheers for that. Already done most of them though.
Actually it wont recognise the objitem hwen you try and include the subfolders but recongizes it fine when you try and apply it to one level.
I don't get it.
__________________ No matter where you go, you are what you are player
And you can try to change but that's just the top layer
Man, you was who you was 'fore you got here http://allannolove.wordpress.com |
| |
21st February 2008, 7:02pm
|
#11 | | Changed Man V4
Join Date: May 2002 Location: Breaking into H
Posts: 32,410
| Re: Script to print all files in folder and subfolders edit: sack that, doesnt work
__________________ You just lost The Game
If animal trapped call 844-6286 |
| |
21st February 2008, 7:46pm
|
#12 | | Changed Man V4
Join Date: May 2002 Location: Breaking into H
Posts: 32,410
| Re: Script to print all files in folder and subfolders strDirectory = "E:\Code\SQL"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(strDirectory)
Set colItems = objFolder.Items
For Each objItem in colItems
objItem.InvokeVerbEx("Edit")
Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objMainFolder = objFSO.GetFolder(strDirectory)
Set colSubfolders = objMainFolder.Subfolders
For Each objSubfolder in colSubfolders
strFolder = objSubfolder.Path
GetFiles strFolder
Next
sub GetFiles(byval strDirectory)
Set objWeeFolder = objShell.Namespace(strDirectory)
Set colWeeItems = objWeeFolder.Items
For Each objWeeItem in colWeeItems
objWeeItem.InvokeVerbEx("Edit")
Next
end sub
This works!
Maybe a bit clumsy, but I was able to go through to a subfolder and loop through the items in that.
It currently only goes down 1 level, but could be changed to loop through other subfolders.
I just sepnt 45 minutes on that, and enjoyed every minute of it.
Work from work, although VBS is fucking old school.
Oh, and obviously change "Edit" to "Print"
__________________ You just lost The Game
If animal trapped call 844-6286
Last edited by Zooropa; 21st February 2008 at 7:48pm.
Reason: Automerged Doublepost
|
| |
22nd February 2008, 1:44pm
|
#13 | | Punisher Moderator
Join Date: Sep 2002 Location: Glasgow
Posts: 9,500
| Re: Script to print all files in folder and subfolders Cool, that's handy.
__________________ No matter where you go, you are what you are player
And you can try to change but that's just the top layer
Man, you was who you was 'fore you got here http://allannolove.wordpress.com |
| |
22nd February 2008, 6:39pm
|
#14 | | Changed Man V4
Join Date: May 2002 Location: Breaking into H
Posts: 32,410
| Re: Script to print all files in folder and subfolders Big Hoody was telling me on MSN he had to copy a file into 900 folders the other day.
"Could I have used VBS for that?"
"Aye."
__________________ You just lost The Game
If animal trapped call 844-6286 |
| |
22nd February 2008, 7:34pm
|
#15 | | Moderator Moderator
Join Date: Jul 2002
Posts: 19,748
| Re: Script to print all files in folder and subfolders It nearly killed me.
__________________ 'CAUSE MUTHAFUCKAS ARE BORED |
| |  | | | Thread Tools | | | | Rate This Thread | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | |