0

I have to extract the names of files from a folder and paste them to a text document (Word document would be ideal but I found a way to link and automatically update a word file with the contents of a text file).

What I was thinking of doing was to have a dialog box open up to ask the user a path to the folder. Then I would use the dir function and paste the values to a text file.

I can get the names but the batch file has to be in the same folder. I wanted the dialog box, is there a way to do that? I didn't really want the user to type in the entire folder path in cmd prompt.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Parth Bakshi
  • 85
  • 1
  • 2
  • 6
  • Just to make sure: You ask for a way to change to one folder/path with the input of a user. This input should not be a full path? – geisterfurz007 Sep 20 '16 at 06:31
  • 1
    Post the code you have at the moment. – Jonas Sep 20 '16 at 07:24
  • What i meant was when the user runs the script, a FileOpenDialog Box opens up where the user can navigate to the respective folder and select it. The code then gets the entire path of the folder that the user selected and sets it as the path from where to get the file names. I dont have any code right now. Just one to get the file name to a text document – Parth Bakshi Sep 20 '16 at 09:01
  • 1
    Batch isn't able to do something like a FileOpenDialogBox without help from another language. – Stephan Sep 20 '16 at 09:03
  • Any other way that the user can enter the folder? Apart from navigating in cmd prompt – Parth Bakshi Sep 20 '16 at 09:07
  • something [like this](http://stackoverflow.com/a/16821671/2152082)? – Stephan Sep 20 '16 at 09:08
  • Or anything else you guys might suggest. I just thought to make the script a little user friendly – Parth Bakshi Sep 20 '16 at 09:09
  • ok then. Try to adapt [it](http://stackoverflow.com/a/16821671/2152082) to your needs and come back if you get stuck. – Stephan Sep 20 '16 at 09:12

1 Answers1

2

This what you need as code to browse for folder and choose it :

Browse4Folder.bat is used from this batch file Local_Batch_Engine.bat

@echo off
Title Browse4Folder
Color 0A
Call :Browse4Folder "Choose source folder to scan" "c:\scripts"
echo You have chosen this location "%Location%"
pause & exit
::***************************************************************************
:Browse4Folder
set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
(
    echo set shell=WScript.CreateObject("Shell.Application"^) 
    echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^) 
    echo if typename(f^)="Nothing" Then  
    echo wscript.echo "set Location=Dialog Cancelled" 
    echo WScript.Quit(1^)
    echo end if 
    echo set fs=f.Items(^):set fi=fs.Item(^) 
    echo p=fi.Path:wscript.echo "set Location=" ^& p
)>%vbs%
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof
::***************************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • 1
    Works like a charm, thank you very much. Now I'll just have to edit it further to get the contents of the folder in a text file. Would you be able to tell me if I can do that to a MS Word document? – Parth Bakshi Sep 21 '16 at 07:30
  • 1
    @ParthBakshi I'm glad that this answer helped you, so you should ask a new question for the last part of your batch and you can link this answer to it ;) You can take a look at this batch file [Local_Search_Engine.bat](http://pastebin.com/ADdjPEfH) it use Browse4Folder and can save a logfile and copy all files found on a folder created on your desktop – Hackoo Sep 21 '16 at 08:02