VortexAdmin
asked on
Quick question about parsing a string in a batch file
Too lazy at the moment to look it up myself. Can someone give me the batch commands to change a Windows path in a string so it can be used with a Unix command?
I need to parse this LocalPath variable....
SET LocalPath=D:\Folder\Subfol der
So I end up with this assigned to a different variable...
ECHO %UnixPath%
Result: /cygdrive/D/Folder/Subfold er
The word "/cygdrive" will remain constant as the first part of the new string, basically I need to get the drive letter and folders, reverse the \ and remove the colon in as few lines as possible.
Thanks!
I need to parse this LocalPath variable....
SET LocalPath=D:\Folder\Subfol
So I end up with this assigned to a different variable...
ECHO %UnixPath%
Result: /cygdrive/D/Folder/Subfold
The word "/cygdrive" will remain constant as the first part of the new string, basically I need to get the drive letter and folders, reverse the \ and remove the colon in as few lines as possible.
Thanks!
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
it translates to:
replace this "/" with (=) that "\"
Do remove the colon (:), we replace this ":" with 'nothing'... (%NewPath::=%)
replace this "/" with (=) that "\"
Do remove the colon (:), we replace this ":" with 'nothing'... (%NewPath::=%)
ASKER
Oops, I've got an extra space in my variable. I need that variable so I can add another folder (variable) to it. Guess I should have given you exactly what I was doing at the beginning but I was trying to make it as simple as possible.
-- I took what you gave me:
SET LocalPath=D:\Folder\Subfol der
set NewPath=%LocalPath:\=/%
set UnixPath=/cygdrive/%NewPat h::=%
-- and changed it to this:
SET LocalPath=D:\Folder\Subfol der
set UnixPath=%LocalPath:\=/%
set UnixPath=/cygdrive/%UnixPa th::=%
-- Later in my batch file, I'll need to add another folder variable to the end with a trailing /, we'll call it ClientName. But adding ClientName to the end puts a space in the path. So this:
SET UnixPath=%UnixPath%/%Clien tName%
produces this:
/cygdrive/D/Folder/Subfold er /ClientName/
Any way I can get that space out of there? Thanks!
-- I took what you gave me:
SET LocalPath=D:\Folder\Subfol
set NewPath=%LocalPath:\=/%
set UnixPath=/cygdrive/%NewPat
-- and changed it to this:
SET LocalPath=D:\Folder\Subfol
set UnixPath=%LocalPath:\=/%
set UnixPath=/cygdrive/%UnixPa
-- Later in my batch file, I'll need to add another folder variable to the end with a trailing /, we'll call it ClientName. But adding ClientName to the end puts a space in the path. So this:
SET UnixPath=%UnixPath%/%Clien
produces this:
/cygdrive/D/Folder/Subfold
Any way I can get that space out of there? Thanks!
didn't see that in my example.
There's another way of doing it that escapes me at this late hour, but you can still use
SET UnixPath=%UnixPath~:0,-1%/ %ClientNam e%
Which takes the UnixPath variable, starting at char 0 all the way to 1 less than the length...(which should eliminate the space).
There's another way of doing it that escapes me at this late hour, but you can still use
SET UnixPath=%UnixPath~:0,-1%/
Which takes the UnixPath variable, starting at char 0 all the way to 1 less than the length...(which should eliminate the space).
ASKER
Hmm,
ECHO %UnixPath% produced:
0,-1/ClientName
Here's what I've got so far:
SET UnixPath=%LocFolder:\=/%
SET UnixPath=/cygdrive/%UnixPa th::=%
SET UnixPath=%UnixPath~:0,-1%/ %ClientNam e%
ECHO %UnixPath%
Thanks.
ECHO %UnixPath% produced:
0,-1/ClientName
Here's what I've got so far:
SET UnixPath=%LocFolder:\=/%
SET UnixPath=/cygdrive/%UnixPa
SET UnixPath=%UnixPath~:0,-1%/
ECHO %UnixPath%
Thanks.
Try this to see where's it's failing...
SET UnixPath=%LocFolder:\=/%
SET UnixPath=/cygdrive/%UnixPa th::=%
echo [%UnixPath%]
SET UnixPath=%UnixPath~:0,-1%/ %ClientNam e%
ECHO [%UnixPath%]
SET UnixPath=%LocFolder:\=/%
SET UnixPath=/cygdrive/%UnixPa
echo [%UnixPath%]
SET UnixPath=%UnixPath~:0,-1%/
ECHO [%UnixPath%]
ASKER
Returned:
[/cygdrive/D/Backups ]
[0,-1ClientName]
Notice that ClientName is the name of the variable, but its actually populated.
ClientName=TestClient
[/cygdrive/D/Backups ]
[0,-1ClientName]
Notice that ClientName is the name of the variable, but its actually populated.
ClientName=TestClient
I don't see where the space is introduced...try without reusing the same variable...
SET UnixPath=%LocFolder:\=/%
SET NewUnixPath=/cygdrive/%Uni xPath::=%
echo [%NewUnixPath%]
SET FinalUnixPath=%UnixPath~:0 ,-1%/%Clie ntName%
ECHO [%FinalUnixPath%]
SET UnixPath=%LocFolder:\=/%
SET NewUnixPath=/cygdrive/%Uni
echo [%NewUnixPath%]
SET FinalUnixPath=%UnixPath~:0
ECHO [%FinalUnixPath%]
ASKER
OK, I did this:
Set LocFolder=D:\MyFolder
Set ClientName=MyClient
SET UnixPath=%LocFolder:\=/%
SET NewUnixPath=/cygdrive/%Uni xPath::=%
echo [%NewUnixPath%]
SET FinalUnixPath=%UnixPath~:0 ,-1%/%Clie ntName%
ECHO [%FinalUnixPath%]
And ended up with this:
[/cygdrive/D/MyFolder]
[0,-1ClientName]
I set the LocFolder and ClientName variables immediately before running the rest of it and it got rid of the space. Before I was pulling them along with 5 others from an outside text file using a command like this:
for /f "tokens=*" %%a in ('find "ClientName=" ^< settings.ini') do set %%a
I checked through all my for /f lines and sure enough at the end of the 3rd one (%%c) was a space. No other character, nothing I could see, just a physical space in the text line where it couldn't be seen.
Thanks for all your help and sticking with me on it!!! I've got it from here.
Set LocFolder=D:\MyFolder
Set ClientName=MyClient
SET UnixPath=%LocFolder:\=/%
SET NewUnixPath=/cygdrive/%Uni
echo [%NewUnixPath%]
SET FinalUnixPath=%UnixPath~:0
ECHO [%FinalUnixPath%]
And ended up with this:
[/cygdrive/D/MyFolder]
[0,-1ClientName]
I set the LocFolder and ClientName variables immediately before running the rest of it and it got rid of the space. Before I was pulling them along with 5 others from an outside text file using a command like this:
for /f "tokens=*" %%a in ('find "ClientName=" ^< settings.ini') do set %%a
I checked through all my for /f lines and sure enough at the end of the 3rd one (%%c) was a space. No other character, nothing I could see, just a physical space in the text line where it couldn't be seen.
Thanks for all your help and sticking with me on it!!! I've got it from here.
ASKER
Thanks!!