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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
fox_cream
Posts: 2
Joined: 17 Apr 2012 01:58

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

#1 Post by fox_cream » 17 Apr 2012 02:16

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
)
)


Last edited by fox_cream on 17 Apr 2012 04:22, edited 1 time in total.

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

Re: Batch Script to execute exe based on file string

#2 Post by foxidrive » 17 Apr 2012 04:08

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"
)
)

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

Re: Batch Script to execute exe based on file string

#3 Post by foxidrive » 17 Apr 2012 04:13

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*)

fox_cream
Posts: 2
Joined: 17 Apr 2012 01:58

Re: Batch Script to execute exe based on file string

#4 Post by fox_cream » 17 Apr 2012 04:21

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.

Post Reply