OK - explaining my post time.. i was meant to cut the code into the code window, not copy it, so ignore the code in the main post :)
Ben
Main Topics
Browse All TopicsHi,
Is it possible to have a Windows Batch script that returns a list of folders in a directory or a list of files in a folder as a string.
I would like to be able to use this functionality in a windows batch editor so the user selects which files to copy to another folder.
Or is this best done with a VBS script.
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Set objFSO = CreateObject("Scripting.Fi
objStartFolder = InputBox( "Enter Your Start Path")
Set objFolder = objFSO.GetFolder(objStartF
Set colSubfolders = objFolder.Subfolders
Set colFiles = objFolder.Files
For Each objSubfolder in colSubfolders
Wscript.Echo objSubfolder.Name
Next
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
Hi,
Thanks for the help.
Will this script work in a batch file, I created a batch file with the VBS code and recieved the following error:
C:\Temp\Test>Set objFSO = CreateObject("Scripting.Fi
C:\Temp\Test>objStartFolder
C:\Temp\Test>Set objFolder = objFSO.GetFolder(objStartF
C:\Temp\Test>Set colSubfolders = objFolder.Subfolders
C:\Temp\Test>Set colFiles = objFolder.Files
C:\Temp\Test>For Each objSubfolder in colSubfolders
How would I out put the string to a file.
Thanks
Hi,
The attached vbs script will display the files in the folder specified in the script.
I can run the vbscript from a bat file like so:
REM Execute the vbscript file
test.vbs
In the code I posted before I was trying to create the vbscript and run it in the same bat file or windows batch script.
I wanted to be able to pass a dir to the script using the windows batch editor and ultimately print a string of file names within that dir/folder
Thanks
crompnk--Rather than reinvent the wheel, I use the very versatile Karen's Directory Printer
http://www.karenware.com/p
crompnk
I'm puzzled. Not by others' suggestions thus far, but by what you stated in your question:
"I would like to be able to use this functionality in a windows batch editor so the user selects which files to copy to another folder."
Qlemo picked up on the "user SELECTS which files..." when he simply said:
"I reckon working on the 'select' part would be what we should do", and AmazingTech also stated: "I guess we don't really understand what you are trying to do...".
The part that puzzles me is where you have more recently explained:
"I wanted to be able to pass a dir to the script using the windows batch editor and ultimately print a string of file names within that dir/folder." That is quite different from how it sounded at the outset, and I'm wondering if this has reached a partial solution but there is more to come.
I'm sorry if I'm sounding a bit dim here, but I don't understand what you mean by the "select", and either displaying to screen or redirecting a list of file names to a file doesn't seem to be fulfilling what you SEEMED to have asked at the outset.
When you refer to "batch editor", are you simply referring to a "batch file"?
I've read the question and all comments several times, and it still seems to me that you are ultimately hoping to get some kind of file selection method, whether it be from one or more input dialogs shown by a VB Script, or by filtering the output of a batch file.
My understanding of the requirements, especially going on your references to "string of file names" and "as a string", is that you are looking to get a STRING comprising a number of file names as a variable for re-use in a COPY command. ie. a list all on one line rather than a list on separate lines.
Please can you put me out of my misery ;-)
Hi,
Sorry for confusing you.
I originally asked if a batch command that would give me a string of folders or files, so for example if the folder was C:\Temp\Test and the files in that folder were:
Test.txt
Test2.txt
Test3.txt
Then the string would be 'Test.txt,Test2.txt,Test3.
The reason I want this is becasue the editor I have is compatable with Windows Batch Script and TSQL. The editor has an interface, which has a control that lets a user select a path (dir) that can be passed to another command to copy files (or something else if needed). This is fine and I can get this working with an easy command, but I wanted the string of files so another control on the interface can list the files that the user can select from if required (only wants to copy certain files).
As the editor is compatable with sql I was thinking I could return the string using an sql statement that calls/uses a batch script/vbscript.
The experts came back with a vbs script that works, but doesn't return a string. It seems to be more complicated than I thought, I was hoping to have a windows batch script or vbscript that returns a string that the tsql could read and print.
I'm not re-inventing the wheel, but rather adding new functionality to an existing interface that is connected to a sql server db.
Thanks for the help and feedback.
Thank you for the explanation crompnk. That clarifies what I thought I was reading, ie. the string of file names.
I will try and figure a way to do this from a batch file, but I'm pretty sure the previous contributors will beat me to it because I must humbly admit that they are far more experienced with batch files than I am. I was really just trying to get a clearer picture of the requirements for everyone's information.
OK guys, how to parse a dir /a-d /b output into a comma-separated text string?
crompnk: can the string be tab or space-separated and double-quoted, or does it have to be comma-separated?
While awaiting your feedback I was quickly messing with a thought that I could create a "file picker" in the CMD window by:
1. Using a FOR command on the results of the DIR /a-d /b /s command (using %%~nxA modifier to output name and extension only perhaps)
2. Pipe results to a FIND /n command (finding the file extension separator dot) to output a list of file names preceded by [1] [2] etc.
3. Prompt the user to enter the numbers against chosen file names
4. FIND each of the lines with matching [1] [2] [3] etc
5. Strip off the [number] from each line leaving file name only
6. Create a string containing comma, tab (or other delimiter) file names as a variable
7. Execute the COPY command passing the variable to provide the parameters.
I did something along these lines years ago in old 16-bit C and Assembly Language for my college exam, so I thought I might make the idea work in DOS. I was having some success, but I am not sure if I will complete it, nor whether it will be usable in your scenario. I'll look again later as I need to sleep now.
jcimarron wrote
"Rather than reinvent the wheel, I use the very versatile Karen's Directory Printer
http://www.karenware.com/p
I also didn't understand the question asked, but I really like this utility. It has a lot of options regarding what to print, or better, save to a file.
One of the side benefits experts opinion is to find utilities like this one.
Amazing Tech
Indeed, very clever stuff you come up with, and some of the other EEs in this DOS area, and I thought I was pretty good at DOS, but I've never tried to get BAT files to do any of the things you're able to force out of the various DOS level commands.
Amazing Tech and others -- Now I have to go back and read some of the other solutions you folks have posted, I'm very impressed.
Thanks for the great posts !!!
Jeff
AmazingTech
Don't you get fed up with the adulation? If not, then I'm impressed as always at the deceptively simple but very clever use of "DOS's" limitations. If cars had never been invented, I'm sure you could have tweaked a triangular-wheeled bicycle to go at 120 mph :-)
I had to use too many workarounds and temp filter files in my much less clever "file picker" batch menu system before you reminded me of Delayed Expansion and had me kicking myself.
crompnk
I'm sure that you will find a working solution from AmazingTech's suggestions to suit your needs. The secret (in Windows XP Batch) to getting a list of files or folders on one line rather than as a DIR listing on separate lines is with the extremely useful EnableDelayedExpansion.
To read more about how it works, type SET /? in a command window and page down about 4 screenfulls.
Thanks for some more great methods, AmazingTech.
Things like this is really easy for me so I really can't see what's the big deal. But thanks for all your appreciation. I always try to make things simple. I hope if I keep it simple others will learn and be able to make their own modifications.
There are also a lot of great experts here and I too have learned things along the way.
For each question I usually start from scratch that really helps in learning the syntax and figuring out new ways to tackle a problem.
Business Accounts
Answer for Membership
by: Pearl_export_benPosted on 2009-06-25 at 04:21:17ID: 24710165
This is done best in VB Script.
leSystemOb ject")
older)
This will do it:
Set objFSO = CreateObject("Scripting.Fi
objStartFolder = "C:\Scripts"
Set objFolder = objFSO.GetFolder(objStartF
Set colSubfolders = objFolder.Subfolders
Set colFiles = objFolder.Files
For Each objSubfolder in colSubfolders
Wscript.Echo objSubfolder.Name
Next
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
This will return all folder names and files:
Select allOpen in new window