Assign %1 Parameter to a set Variable for Manipulation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
whdyck
Posts: 3
Joined: 21 Aug 2012 15:18

Assign %1 Parameter to a set Variable for Manipulation

#1 Post by whdyck » 21 Aug 2012 15:30

Hi,

I have a subroutine that receives as a parameter a filename that looks like
cgc.lia.20120821.081234

I'd like to substitute a hyphen for those periods and tack on a ".csv" (so I can import the file into Access). My code looks like this:

Code: Select all

:ProcessLIA

rem Run the file parser
..\awk.exe -v CurrDateTime=%CurrDateTime% -v LogFile=%LogFile% -f ..\lia.awk %1 > ..\temp.csv

if %errorlevel% EQU 0 (
  rem Success, so append to the master file and move the source file to archive
  rem Save as .txt so that Excel can open it with all columns in text format
  type ..\temp.csv > ..\CSVInput\%1.csv
  rem move %1 %ArchiveFolder%
)

rem Delete the temp file
del /q ..\temp.csv
pause
rem Exit from subroutine
goto :EOF


Ideally, I'd like to replace the "%1.csv" above with a filename that substitutes the periods with hyphens (all except the last period before the "csv").

Is this possible?

Thanks for any help you can give.

Wayne

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Assign %1 Parameter to a set Variable for Manipulation

#2 Post by abc0502 » 21 Aug 2012 16:11

This was not tested
notice the 3rd line and all %1 replaced with %file%
The file name consist of 4 tokens separated by a dot, so use a for command to get tokens values,Then separate each token with what you need.
The result then can be assignned to a new variable that will be used later

Code: Select all

:ProcessLIA

For /F "tokens=1,2,3,4* delims=." %%a in ("%1") Do set "file=%%a-%%b-%%c-%%d"
rem Run the file parser
..\awk.exe -v CurrDateTime=%CurrDateTime% -v LogFile=%LogFile% -f ..\lia.awk %file% > ..\temp.csv

if %errorlevel% EQU 0 (
  rem Success, so append to the master file and move the source file to archive
  rem Save as .txt so that Excel can open it with all columns in text format
  type ..\temp.csv > ..\CSVInput\%file%
  rem move %file% %ArchiveFolder%
)

rem Delete the temp file
del /q ..\temp.csv
pause
rem Exit from subroutine
goto :EOF

If it returns a file with no extention, add .csv at the end of %file%

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Assign %1 Parameter to a set Variable for Manipulation

#3 Post by Squashman » 21 Aug 2012 18:24

Why not just assign %1 to a variable and then use string substitution.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Assign %1 Parameter to a set Variable for Manipulation

#4 Post by abc0502 » 21 Aug 2012 18:35

Squashman wrote:Why not just assign %1 to a variable and then use string substitution.


:lol: that is right
I missed that :oops:

whdyck
Posts: 3
Joined: 21 Aug 2012 15:18

Re: Assign %1 Parameter to a set Variable for Manipulation

#5 Post by whdyck » 21 Aug 2012 20:56

Yes, I tried that, and I couldn't get it to work. Maybe I'm just making a bonehead error in my syntax.

Here's what I tried:

Code: Select all

for %%f in ("???.lia.*") do call :ProcessLIA %%f
.
.
.
:ProcessLIA

rem Run the file parser
..\awk.exe -v CurrDateTime=%CurrDateTime% -v LogFile=%LogFile% -f ..\lia.awk %1 > ..\temp.csv

if %errorlevel% EQU 0 (
  rem Success, so append to the master file and move the source file to archive
  set fname=%1
  echo fname=%fname%
  type ..\temp.csv > ..\CSVInput\%1.csv
  rem move %1 %ArchiveFolder%
)

rem Delete the temp file
del /q ..\temp.csv
pause
rem Exit from subroutine
goto :EOF


Problem is that the "echo fname" statement, when executed, displays the following:
fname=

Is my statement incorrect where I assign %1 to fname? (The other parts of the script are working, and a file is actually getting processed in the loop.)

Wayne

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

Re: Assign %1 Parameter to a set Variable for Manipulation

#6 Post by foxidrive » 21 Aug 2012 23:24

whdyck wrote:Problem is that the "echo fname" statement, when executed, displays the following:
fname=


You need to use delayed expansion and then !fname! instead of %fname%
Be aware that it has some limitations: such as ! characters break it.

Or you could set fname=%1 before the loop begins.

whdyck
Posts: 3
Joined: 21 Aug 2012 15:18

Re: Assign %1 Parameter to a set Variable for Manipulation

#7 Post by whdyck » 22 Aug 2012 09:28

foxidrive wrote:You need to use delayed expansion and then !fname! instead of %fname%
Be aware that it has some limitations: such as ! characters break it.

Or you could set fname=%1 before the loop begins.


That worked! I had never heard of delayed expansion. :D

Thanks very much for the help.

Wayne

Post Reply