• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

SOLVED Batch convert files

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Silver_Pharaoh

Likes the big ones n00b Member
Joined
Sep 7, 2013
So I have ~300 files that I need to convert with a program.

to convert these files manually, I'd just type in:

convert.exe -o outputfilename.wav inputfile.dsp

-o = output file
inputfile.dsp is the file that needs to be converted.



All the files end in .dsp
And as you can see, I'd rather not type in that command 300 times.
So I'm wondering if there is a way to automate this process of typing in the output file name and what file to input.


A for loop might work I suppose, but I'm not that great with scripting. :-/
The biggest issue I see is automating the input & output file names.
Ideally they would be the same. (Just the different extension of course ;) )

I have Powershell 3.0 installed and of course, trusty old notepad for making batch files! :D

Thanks guys!
 
Code:
@ECHO OFF
cd "C:\Users\username\Desktop\Folder"
for /F %%A in ('dir /b *.dsp') DO convert.exe %%~nA.wav %%A

That should do the trick.
Save the above as a batch file (Update the cd folder) and run.

You can test by making the command
Code:
@ECHO OFF
cd "C:\Users\username\Desktop\Folder"
for /F %%A in ('dir /b *.dsp') DO ECHO convert.exe %%~nA.wav %%A
instead.
 
Last edited:
YOU ARE A GOD :ty: :ty:
It works!!!

Now if you can (please) explain it?
for /F %%A in ('dir /b *.dsp') DO convert.exe %%~nA.wav %%A

For future knowledge, I'd like to understand what the code in pink does.

Like, I don't know what %%A does. Some sort of flag?



Thanks man you saved me a HUGE headache!
 
Essentially:
Code:
for /F %%A in ('dir /b *.dsp')
- Runs the Command dir /b *.dsp (Which returns a list of all *.dsp files in the current folder)
- for /F %%A in then loops through each result and assigns each item in the list to the variable %%A

Code:
DO convert.exe %%~nA.wav %%A
- Each item in the list is processed by the above command
- %%~nA returns only the file name (not extension) of the variable %%A


That should be clear as mud :-S
 
Essentially:
Code:
for /F %%A in ('dir /b *.dsp')
- Runs the Command dir /b *.dsp (Which returns a list of all *.dsp files in the current folder)
- for /F %%A in then loops through each result and assigns each item in the list to the variable %%A

Code:
DO convert.exe %%~nA.wav %%A
- Each item in the list is processed by the above command
- %%~nA returns only the file name (not extension) of the variable %%A


That should be clear as mud :-S

Ahh!
Thanks a bunch man!

All those .dsp files are converted now. :cool:

Thread solved!
 
Very cute indeed.

Is this just a DOS batch file or you need something else?
 
Very cute indeed.

Is this just a DOS batch file or you need something else?

I was thinking that I'd have to pull up my socks and make a powershell script :p
But the simple batch file Realee made worked like a charm! :thup:

Basically the batch file gets the names of files in a directory, stores it in a variable and then runs the program, setting the input and output filenames for the program with the names in the variable.

I also imagine with a bit of tweaking, one could use that batch file as a template for other programs than need a file name entered like that. :)
 
Just for fun, a powershell equivalent might look like this (untested, but ought to be pretty close):

Code:
$EXE = "C:\path\to\convert.exe"
$WORKDIR = "C:\path\to\your\dsp\files"

foreach ($f in Get-ChildItem $WORKDIR\*.dsp)
{
& $EXE -o $($f.basename + ".wav") $f.name
}
 
Just for fun, a powershell equivalent might look like this (untested, but ought to be pretty close):

Code:
$EXE = "C:\path\to\convert.exe"
$WORKDIR = "C:\path\to\your\dsp\files"

foreach ($f in Get-ChildItem $WORKDIR\*.dsp)
{
& $EXE -o $($f.basename + ".wav") $f.name
}

Thanks!

Throws errors though :-/

Let me update to Powershell 3.0 and see if that helps. (I forgot to update Powershell after my fresh install :facepalm: )
 

Attachments

  • Capture.PNG
    Capture.PNG
    24.6 KB · Views: 38
Oh, it's because it can't find the file because you're not in that directory. You can either add a 'cd $WORKDIR' outside of the foreach loop, or change your input and output files to $WORKDIR\$f.name and $WORKDIR\$f.basename. Changing the directory to your workdir is probably easiest:

Code:
$EXE = "C:\path\to\convert.exe"
$WORKDIR = "C:\path\to\your\dsp\files"

cd $WORKDIR
foreach ($f in Get-ChildItem $WORKDIR\*.dsp)
{
& $EXE -o $($f.basename + ".wav") $f.name
}
 
Oh, it's because it can't find the file because you're not in that directory. You can either add a 'cd $WORKDIR' outside of the foreach loop, or change your input and output files to $WORKDIR\$f.name and $WORKDIR\$f.basename. Changing the directory to your workdir is probably easiest:

Code:
$EXE = "C:\path\to\convert.exe"
$WORKDIR = "C:\path\to\your\dsp\files"

cd $WORKDIR
foreach ($f in Get-ChildItem $WORKDIR\*.dsp)
{
[COLOR="Pink"]&[/COLOR] $EXE -o [COLOR="Pink"]$($f.basename + ".wav")[/COLOR] $f.name
}

Yep! That fixed it!

Now The stuff in pink...
What do they do?

Not sure what the & has to do with anything..
And the $($f.basename + ".wav"), why the $ and the brackets?


I was taught scripting just last semester. Programming was NEVER a strong point for me, so needless to say, that scripting course was no walk in the park for me.

So if the script was broken down into bits for me, I can understand it better :)
Thanks :D
 
The ampersand is just Powershell's syntax to call an external executable. Honestly, it may or may not even be necessary if you give the fully qualified path to the exe, but I've always used it.

The $ is powershell's variable character, (similar to % in batch). The reason for it and the ()'s is because of the way Powershell expands variables. If you were just looking for the filename, you could just use "$f.name" (which would give you the full name and extension). Since we want to strip the ".dsp" and replace it with ".wav", we only need the basename of the file, hence $f.basename, which just gives the filename without an extension. Then we add ".wav" to the end of the basename to give us the new filename, but Powershell doesn't understand "+" or ".wav" by themselves, so you have to wrap them in something it can understand, $().


The syntax for Powershell seems sort of esoteric at first, but once you work with it a bit more frequently you'll start to understand it. Of course, if you have a good grasp on OOP to begin with, that certainly helps the learning curve. :)
 
The ampersand is just Powershell's syntax to call an external executable. Honestly, it may or may not even be necessary if you give the fully qualified path to the exe, but I've always used it.

The $ is powershell's variable character, (similar to % in batch). The reason for it and the ()'s is because of the way Powershell expands variables. If you were just looking for the filename, you could just use "$f.name" (which would give you the full name and extension). Since we want to strip the ".dsp" and replace it with ".wav", we only need the basename of the file, hence $f.basename, which just gives the filename without an extension. Then we add ".wav" to the end of the basename to give us the new filename, but Powershell doesn't understand "+" or ".wav" by themselves, so you have to wrap them in something it can understand, $().


The syntax for Powershell seems sort of esoteric at first, but once you work with it a bit more frequently you'll start to understand it. Of course, if you have a good grasp on OOP to begin with, that certainly helps the learning curve. :)

Awesome! :thup:
Thanks man. I appreciate your script & explanation.

I knew Powershell could do it, I was just unsure on how to approach it. :p
 
So today I was uploading some pictures from my new camera (Nikon D3200), and resizing them... thinking "There's got to be a better way.."

So I google for command line resizing utilities, first thing that comes up is ImageMagick's 'convert.exe' and I thought to myself: "This seems oddly familiar." Came back here and lifted the Powershell code I gave you above and am using it for myself now to shrink my images to a more reasonable, web-friendly size.
 
Last edited:
So today I was uploading some pictures from my new camera (Nikon D3200), and resizing them... thinking "There's got to be a better way.."

So I google for command line resizing utilities, first thing that comes up is ImageMagick's 'convert.exe' and I thought to myself: "This seems oddly familiar." Came back here and lifted the Powershell code I gave you above and am using it for myself now to shrink my images to a more reasonable, web-friendly size.

:D :thup:
Hehe, that's awesome!
 
Back