cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1640
Views
0
Helpful
1
Replies

Passing variable values containing spaces to windows-based command script

Joe Fletcher
Level 1
Level 1

Hi,

 

I would imagine someone has solved this before hopefully. I have a job running on a windows agent (master is 6.2.1 on Linux) which is intended to rename a file.

The job stream is driven by a file event so we have a variable GROUP_FILENAME=<Filemon.Filename> picked up from the file event.

The source file sits in a windoze shared drive. Step one is to transfer the file to a unix system. This step works fine. We then wish to rename the source file to add a time based on <sysdate><systime>.

 

I have a command script rename.bat which is called with two arguments <Group.GROUP_FILENAME> and <Group.GROUP_FILENAME><sysdate><systime>

 

The script is just

 

rename %1 %2

 

The problem comes when GROUP_FILENAME has a space in the filename value.

How do I get the entire variable passed intact?

For example: GROUP_FILENAME = Report Daily.mmddyy.xlsx

 

When the script gets called, regardless of whatever combination of quotes and escapes I try I get an error:

 

file "Report" not found.

 

Is there some magic combination of "" \ escapes or something needed to pass the variable unchanged to the command script? For info I've also tried using gnuwin32's mv.exe (port of the unix mv command) but the results are largely the same.

 

 

 

1 Reply 1

look at this code:

 

@Echo off
setlocal EnableDelayedExpansion

 

REM --------------------------------

IF not "%~1"=="" GOTO parm_1_entered

:no_parm_1_entered
ECHO rename-test.bat requires parm 1 for the old name of the file
EXIT /b 98

:parm_1_entered
SET file_1=%~1

 

REM --------------------------------

IF not "%~2"=="" GOTO parm_2_entered

:no_parm_2_entered
ECHO rename-test.bat requires parm 2 for the new name of the file
EXIT /b 99

:parm_2_entered
SET file_2=%~2

 

REM --------------------------------

ECHO REN "%file_1%" "%file_2%"

EXIT /b %ERRORLEVEL%

 

 

execute the code with: BAT_NAME "file one" file two"

 

C:\>dir ren*
Volume in drive H is Users
Volume Serial Number is 80AE-90F5

Directory of C:\

09/10/2018 02:41 PM 568 rename-test.bat
1 File(s) 568 bytes
0 Dir(s) 310,235,996,160 bytes free

 

C:\>rename-test.bat
rename-test.bat requires parm 1 for the old name of the file

 

C:\>rename-test.bat a
rename-test.bat requires parm 2 for the new name of the file

 

C:\>rename-test.bat a b
REN "a" "b"

 

C:\>rename-test.bat "file one" "file two"
REN "file one" "file two"

 

 

refer to this link:

https://stackoverflow.com/questions/40588910/windows-batch-passing-arguments-with-spaces