Issue using Variable for filename in Command line

Feel free to ask any question here
Post Reply
pfisk
Posts: 15
Joined: Thu Sep 13, 2018 9:08 pm

Issue using Variable for filename in Command line

Post by pfisk »

I have a simple batch file script as shown below which takes one command line parameter %1 (i.e full point cloud file name)
REM START ===========================================
set /A sfIndex1 = 0
set /A sfIndex2 = %sfIndex1% + 1
SET filedrive=%~d1
SET filepath=%~p1


REM STEP 1: Open file specified in command line parameter then
REM Export X and Y Coordinate to Scalar field (creates Scalar field [Coord. X] and [Coord. Y])
REM then save as new file called: Step1.bin
REM NOTE the %1 command line variable works fine with or without spaces in the file path/name

"C:\Program Files\CloudCompare\CloudCompare.exe" -SILENT -O %1 -AUTO_SAVE OFF -COORD_TO_SF X -COORD_TO_SF Y -SAVE_CLOUDS FILE 'Step1.bin'
SET OUTPUTFILE=%filedrive%%filepath%Step1.bin
REM STEP 1 works fine and 'Step1.bin' gets created in the same directory as the original source file.
REM OUTPUTFILE variable correctly contains the full file path to Step1.bin (Path has spaces in it)


REM Step 2: Tank file specified in OUTPUTFILE variable as input and do SF arithmetic on it and save as new bin file
"C:\Program Files\CloudCompare\CloudCompare.exe" -SILENT -O %OUTPUTFILE% -AUTO_SAVE OFF -SF_OP %sfIndex1% pow2 -SF_OP %sfIndex2% pow2 -SAVE_CLOUDS FILE 'Step2.bin'
REM END===================================

PROBLEM
%OUTPUTFILE% has spaces in it which cause the command line to get broken and CC cant open the file
I have tried to put single or double quotes around the variable like this:
'%OUTPUTFILE%'
"%OUTPUTFILE%"
but neither of them work.
If I take the variable out and put a hard coded path with double quotes around it, then it works.
i.e.
"C:\Program Files\CloudCompare\CloudCompare.exe" -SILENT -O "C:\Temp Folder\Step1.bin" -AUTO_SAVE OFF -SF_OP %sfIndex1% pow2 -SF_OP %sfIndex2% pow2 -SAVE_CLOUDS FILE 'Step2.bin'

Bottom line is I need to figure out how to generate this command-line with a variable for the filename.

Any suggestions


Here is the actual command from the Console window:
C:\Users\pauld\Downloads\BATCH TEST>SET OUTPUTFILE=C:\Users\pauld\Downloads\BATCH TEST\Step1.bin
C:\Users\pauld\Downloads\BATCH TEST>"C:\Program Files\CloudCompare\CloudCompare.exe" -SILENT -O C:\Users\pauld\Downloads\BATCH TEST\Step1.bin -AUTO_SAVE OFF -SF_OP 0 pow2 -SF_OP 1 pow2 -SAVE_CLOUDS FILE 'Step2.bin'

Then CC error: (You can see the space in the path name breaks the command)
[LOADING]
Opening file: 'C:\Users\pauld\Downloads\BATCH'
[Load] Can't guess file format: no file extension
Processed finished in 0.00 s.
daniel
Site Admin
Posts: 7414
Joined: Wed Oct 13, 2010 7:34 am
Location: Grenoble, France
Contact:

Re: Issue using Variable for filename in Command line

Post by daniel »

Maybe something worth to try (following https://stackoverflow.com/questions/185 ... batch-file): can you try to add quotes around %~d1 and %~p1 after 'SET XXX=" and also in CC's command?

e.g. SET filedrive="%~d1" and -O "%OUTPUTFILE%"
Daniel, CloudCompare admin
pfisk
Posts: 15
Joined: Thu Sep 13, 2018 9:08 pm

Re: Issue using Variable for filename in Command line

Post by pfisk »

Fantastic. I got it to work by putting all pieces of the filename into a variable like this:
SET inputFile="%filedrive%%filepath%Step1.bin"
ECHO Input File is %inputFile%
"C:\CloudCompare.exe" -SILENT -O %inputFile% -AUTO_SAVE OFF -SF_ARITHMETIC 1 pow2 -SF_ARITHMETIC 2 pow2 -SAVE_CLOUDS FILE 'Step2.bin'

Thanks for leading me to a solution

Paul
Post Reply