Passing a variable from Excel to a batchfile

Running a batch file from excel

The code in excel is:

set arg1 %1

if [%1] equ [] set arg1=%cd:~0,1%

echo "Test" %arg1%

echo %arg1%:\ZZZ\ZIP-BAT\source-zip.txt

dir e:\ZZZ\Zip-Bak\DayBkup\hm18*.zip>%arg1%:\ZZZ\ZIP-BAT\source-zip.txt

dir %arg1%:\ZZZ\Zip-Bak\DayBkup\hm*.zip>%arg1%:\ZZZ\ZIP-BAT\dest-zip.txt

The Dos batch file content is

set arg1 %1

if [%1] equ [] set arg1=%cd:~0,1%

echo "Test" %arg1%

echo %arg1%:\ZZZ\ZIP-BAT\source-zip.txt

dir e:\ZZZ\Zip-Bak\DayBkup\hm18*.zip>%arg1%:\ZZZ\ZIP-BAT\source-zip.txt

dir %arg1%:\ZZZ\Zip-Bak\DayBkup\hm*.zip>%arg1%:\ZZZ\ZIP-BAT\dest-zip.txt


I get an error Environment variable not defined (see attached screen shot)

Need help

Thanks
Dos-Batch.JPG
rogerdjrAsked:
Who is Participating?
 
oBdACommented:
You forgot the equal sign in the assignment in line 1.
And do yourself a favor and use "speaking" variables.
Try it like this:
set drive=%~1
if "%~1"=="" set drive=%cd:~0,1%
echo "Test" %drive%
echo %drive%:\ZZZ\ZIP-BAT\source-zip.txt
dir "e:\ZZZ\Zip-Bak\DayBkup\hm18*.zip">"%drive%:\ZZZ\ZIP-BAT\source-zip.txt"
dir "%drive%:\ZZZ\Zip-Bak\DayBkup\hm*.zip">"%drive%:\ZZZ\ZIP-BAT\dest-zip.txt"

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.