(SOLVED) Help creating a batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
icon123
Posts: 9
Joined: 13 Sep 2012 09:58

(SOLVED) Help creating a batch file

#1 Post by icon123 » 13 Sep 2012 10:10

Hi, I am trying to write a batch file that will take all file names in a directory that match *.ext and put them in a command line. The command line is:

program.exe file1.ext + file2.ext + file3.ext + ...

I have been trying a few different things with for loops, but I can't figure this out. Thanks for any help.
Last edited by icon123 on 11 Oct 2012 22:00, edited 6 times in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Help creating a batch file...

#2 Post by abc0502 » 13 Sep 2012 10:24

Code: Select all

@echo off & cls

For /F "tokens=*" %%A in ('Dir /B /A:-D "*.txt"') Do (
   < nul set /p "=%%A + ">>temp.txt
)

For /F "tokens=*" %%B in ('type "temp.txt"') Do set line=%%B

echo Program.exe %line%
Del "temp.txt" >nul

pause

but there is an extra + sign at the end i couldn't get it removed
Last edited by abc0502 on 13 Sep 2012 10:32, edited 2 times in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating a batch file...

#3 Post by foxidrive » 13 Sep 2012 10:27

Try this:

Code: Select all

@echo off
for %%a in (*.ext) do call set var=%%var%% + "%%a"
set "var=%var:~2%"
echo %var%
program.exe %var%
pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating a batch file...

#4 Post by foxidrive » 13 Sep 2012 10:31

abc0502 wrote:

Code: Select all

Del /F /S /Q "temp.txt" >nul




Be very careful where you use /S and the del command.

It's not needed to delete a file that is going to exist, so just use

Code: Select all

Del "temp.txt"

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Help creating a batch file...

#5 Post by abc0502 » 13 Sep 2012 10:32

foxidrive wrote:Try this:

Code: Select all

@echo off
for %%a in (*.ext) do call set var=%%var%% + "%%a"
set "var=%var:~2%"
echo %var%
program.exe %var%
pause


that is nice :)
and thanks for the note i will modify it

icon123
Posts: 9
Joined: 13 Sep 2012 09:58

Re: Help creating a batch file...

#6 Post by icon123 » 13 Sep 2012 11:05

Thank you both!

Looking at foxidrive's example, it appears that whe the %var% is added to the command line, it is missing the first " around the 1st value. i.e. the command line becomes:

program.exe file1.ext"+"file2.ext"+"file3.ext" ...


Edit: I added a " to the command line:

Code: Select all

program.exe "%var%


and now it works great. Thanks again.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating a batch file...

#7 Post by foxidrive » 13 Sep 2012 11:11

There must be another reason, as here it returns this:

Code: Select all

 "find.txt" + "printerlist.txt" + "printers.txt" + "replace.txt" + "replace2.txt"


Perhaps you edited the code...

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Help creating a batch file...

#8 Post by abc0502 » 13 Sep 2012 11:14

Foxidrive code work fine with me, and there is no missing quotes, but there is 2 spaces between the program.exe and the first quote at the first file

icon123
Posts: 9
Joined: 13 Sep 2012 09:58

Re: Help creating a batch file...

#9 Post by icon123 » 13 Sep 2012 11:57

I had to take the spaces out between the " and the +, maybe that did it. Regardless, its working great. Thank you so much, I spent a lot of time on that one.

Now for a tweak...

Is there anyway I can modify it to only use the files that I drag and drop onto the .bat file? Currently, if I drag and drop a few of the files in the directory, it will still process all the files with *.ext that are in the directory. Thanks again.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating a batch file - How can I make it drag and

#10 Post by foxidrive » 13 Sep 2012 23:09

This has to use full paths as the batch file could be anywhere.

Code: Select all

@echo off
:loop
set var=%var%+"%~1"
shift
if not "%~1"=="" goto :loop
set "var=%var:~1%"
echo "c:\my folder\program.exe" %var%
pause

icon123
Posts: 9
Joined: 13 Sep 2012 09:58

Re: Help creating a batch file - How can I make it drag and

#11 Post by icon123 » 14 Sep 2012 08:02

Works great, thanks. What would I have to add to the script to move the files that I drag and dropped to another directory? For example if the files that I drag and drop from is in directory \dir1, I want to move the processed files to \dir1\dir2. Thanks again.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating a batch file - How can I make it drag and

#12 Post by foxidrive » 14 Sep 2012 08:37

It depends on your program.exe as often you can specify an output path.

icon123
Posts: 9
Joined: 13 Sep 2012 09:58

Re: Help creating a batch file - How can I make it drag and

#13 Post by icon123 » 14 Sep 2012 08:49

I want to move the source files.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help creating a batch file - How can I make it drag and

#14 Post by foxidrive » 14 Sep 2012 09:01

Try this:

Code: Select all

@echo off
md "%~dp1\dir2" 2>nul
set num=0
:loop
set /a num=num+1
set var=%var%+"%~1"
set file~%num%="%~1"
shift
if not "%~1"=="" goto :loop
set "var=%var:~1%"
echo "c:\my folder\program.exe" %var%
for /f "tokens=1,* delims==" %%a in ('set file~') do (
move /y "%%~b" "%%~dpb\dir2" >nul
set %%a=
)
pause

icon123
Posts: 9
Joined: 13 Sep 2012 09:58

Re: Help creating a batch file - How can I make it drag and

#15 Post by icon123 » 14 Sep 2012 09:17

Awesome, thanks so much! I have one more request. Can you tell me how I would modify the below script for drag and drop and moving the source files as well. Similar to what you just did with the other script. Thanks.

Code: Select all

@SET mkvmerge_PATH=C:\mkvtoolnix\mkvmerge.exe
@SET eac3to_PATH=C:\eac3to\eac3to.exe

for /f "delims=;" %%a in ('dir *.mts /b') do (

   %eac3to_PATH% "%%a" "%%~na.h264" "%%~na.ac3" "%%~na.sup"

   %MKVMERGE_PATH% -o "%%~na.mkv" --forced-track 0:no --compression 0:none -d 0 -A -S -T --no-global-tags --no-chapters "%%~na.h264" --forced-track 0:no --compression 0:none -a 0 -D -S -T --no-global-tags --no-chapters "%%~na.ac3" --forced-track 0:no -s 0 -D -A -T --no-global-tags --no-chapters "%%~na.sup" --track-order 0:0,1:0,2:0

)

Post Reply