You are not logged in.

#1 28 May 2014 00:05

peterbollwerk
New Member
Registered: 27 May 2014
Posts: 3

Batch file "for do" loop problems

I am new to scripting in general and am trying to figure out how to batch encrypt files for us to SFTP to a client.
I've dug around many forums and articles to figure out the right commands to do this and have come up with this (which unfortunately doesn't work):

setlocal EnableDelayedExpansion

REM input dir
SET gpginput=C:\temp\gpgtestin\

REM output dir
SET gpgoutput=C:\temp\gpgtestout\

REM processed dir
SET gpgprocessed=C:\temp\gpgprocessed\

REM Sets working directory to previously defined variable "gpginput"
pushd %gpginput%

REM Clears variable, in case this was not cleared from a previous run
Set FileToEncrypt=

REM Looks for files in the current working directory and sets the filename to the variable FileToEncrypt, 
REM encrypts the file, outputs the file to the directory set in "gpgoutput", 
REM then finally moves the original file also to the processed directory,
REM to indicate the file has been processed.

for /f %%a in ('dir /b') do (
	set FileToEncrypt=%%a
	set gpgfileoutput=%gpgoutput%%FileToEncrypt%
	gpg -o %gpgfileoutput%.gpg -r Peter -e %FileToEncrypt%
	move %%a %gpgtestout%
)
endlocal

What happens is that the script gets stuck, a single ".gpg" file gets created in the input directory (no actual filename), nothing is created in the output directory and no files are moved to the processed directory. (I have 2 test files in the input directory)
Any idea where I went wrong?

Thanks in advance.

Offline

#2 28 May 2014 00:17

peterbollwerk
New Member
Registered: 27 May 2014
Posts: 3

Re: Batch file "for do" loop problems

I doubt it matters, but this is on 64-bit Win7.

Offline

#3 28 May 2014 06:39

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: Batch file "for do" loop problems

When using delayed expansion you need to use ! instead of % when changing and using variables inside a loop

	set gpgfileoutput=!gpgoutput!!FileToEncrypt!
	gpg -o !gpgfileoutput!.gpg -r Peter -e !FileToEncrypt!
	move %%a !gpgtestout!

Offline

#4 28 May 2014 16:22

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Batch file "for do" loop problems

What foxidrive said, also you haven't defined "%gpgtestout%" in the code posted.


cmd | *sh | ruby | chef

Offline

#5 28 May 2014 17:16

peterbollwerk
New Member
Registered: 27 May 2014
Posts: 3

Re: Batch file "for do" loop problems

Thanks so much for the help.
It works great now!

You folks rock!

Offline

Board footer

Powered by