Page 1 of 1

Batch Script to execute exe based on file string [SOLVED]

Posted: 17 Apr 2012 02:16
by fox_cream
Hi,

First off, I'm very new to batch, but have varied experience in other languages. I have a number of files that taken of an encrypted FTP server that need to be run through an encryption programme. There's a large number of files so I need a script to do this. I can get the script to loop through the files and execute the decryption fine (with the appropriate key), but I want to be able to run the decrypt only if the first 3 letters of the filename are "mar". The encryption executable is in the same folder as the files and the output just appends .xls to the file name.

I'm seriously stumped here as based on everything I've read what I have below should work. I'm running on winXP 32bit sp3 if that makes a difference. Thanks for any help,

Code: Select all

 :: file decryption
@echo on

:: loop through files to decrypt

for %%x in (*) do (
set var=%%x:~0,3%
@echo %var%
if "%var%"=="mar" (
des -D -u -k "ENCRYPTION KEY" %%x %%x:~-8.xls
)
)



Re: Batch Script to execute exe based on file string

Posted: 17 Apr 2012 04:08
by foxidrive
This should work - it will probably have issues with ! in filenames.

Code: Select all

:: file decryption
@echo on

:: loop through files to decrypt
setlocal enabledelayedexpansion
for /f "delims=" %%x in ('dir * /b ') do (
set var=%%x
set var1=!var:~0,3!
set var2=!var:~-8!
echo !var1
if /i "!var1!"=="mar" (
des -D -u -k "ENCRYPTION KEY" "%%x" "!var2!.xls"
)
)

Re: Batch Script to execute exe based on file string

Posted: 17 Apr 2012 04:13
by foxidrive
You can also eliminate the test for mar by using dir mar* /b

and if you use this below then you don't need the for /F syntax, assuming no files have names like marmar

for %%x in (mar*)

Re: Batch Script to execute exe based on file string

Posted: 17 Apr 2012 04:21
by fox_cream
that worked perfectly foxidrive - no filename will have a ! in it so that's not an issue. Thank you so much for your quick help - it's very much appreciated.