batch cmd parsing and rewriting an ini file - parsing

I'm trying to make changes to an existing ini file and then rewrite those changes to a new ini file. The problem I am having is parsing the sections doesn't seem to work. If I use the following code:
set reso=%2
set RESLINE=RESMAX=0.72
set nRESLINE=RESMAX=%reso%
setlocal enableDelayedExpansion
(
for /F "tokens=* delims=" %%a in ('findstr "^" %INIFILE%') do (
set "lines=%%a"
if defined lines set "lines=!lines:%p4p%=%new%!"
if defined lines set "lines=!lines:%RESLINE%=%nRESLINE%!"
echo(!lines!
)
) > new.ini
I get out a new.ini file which when the %p4p% variable is found in line, it replaces it with %new% which is fine.
But when the RESLINE hits my output shows:
0.72=RESMAX=0.70=0.72
instead of:
RESMAX=0.70
as desired. I suspect this is because of the "=" being present in the strings, but I'm not sure how to get around it.
What I've tried doing instead is the following:
setlocal enableDelayedExpansion
(
for /F "tokens=* delims=" %%a in ('findstr "^" %INIFILE%') do (
set "lines=%%a"
if defined lines set "lines=!lines:%p4p%=%new%!"
for /f "tokens=1,2 delims==" %%y in ('%lines%') do (
if %%y==RESMAX (
set "oRES=%%z"
set "lines=!lines:%oRES%=%reso%!"
)
)
echo(!lines!
)
) > test.ini
But it seems in this case that the nested for doesn't ever read in the right information.
I assumed what I was attempting here was to parse the read in line which should be of the format PARAM=VALUE, then check if the PARAM is = the word RESMAX and if it is, assign its corresponding value to the var oRES and then set the read-in line to now say RESMAX=newvalue. I've tried replacing the %lines% with !lines! but that just gets me a series of errors saying the various ini params don't exist as acceptable programs/batchs.
Any help would be much appreciated.
Thanks

set "reso=%2"
set "old_reso=0.72"
set "RESLINE=RESMAX=0.72"
set "nRESLINE=RESMAX=%reso%"
setlocal enableDelayedExpansion
(
for /F "tokens=* delims=" %%a in ('findstr "^" %INIFILE%') do (
set "lines=%%a"
if defined lines set "lines=!lines:%p4p%=%new%!"
if defined lines (
rem set "lines=!lines:%RESLINE%=%nRESLINE%!"
echo !lines! | find /i "%RESLINE%" >nul 2>&1 && (
set "lines=!lines:%old_reso%=%reso%!"
)
)
(echo(!lines!)
)
) > new.ini
Try this. Or check dbenham's REPLVAR (the problem is that you cant directly replace the = sign)

Related

Comparison of strings using variables always false (in Batch)

In my current folder, I have a file named backup(yyyy-mm-dd).7z
I need to keep that length variable even though here it is a constant.
#echo off
setlocal EnableDelayedExpansion
set "a=.\backup"
set "b=yyyy-mm-dd"
set "length=19"
for %%f in (.\*) do (
set "fullpath=%%f"
set "trimpath=!fullpath:~0,%length%!"
set trimpath
echo trimpath=%trimpath%
if %trimpath% == %a%(%b% echo this is equal
)
I have 2 questions regarding that code:
Why can I see the value of trimpath when I call set trimpath but not directly with %trimpath%?
Why is my condition false? What should I do to get it true?
Here is the solution after following #foxidrive and #JosefZ advices!
#echo off
setlocal EnableDelayedExpansion
set "a=.\backup"
set "b=yyyy-mm-dd"
set "length=19"
for %%f in (.\*) do (
set "fullpath=%%f"
set "trimpath=!fullpath:~0,%length%!"
echo trimpath=!trimpath!
if "!trimpath!" == "%a%(%b%" echo this is equal
)
If you need to test the solution, just create a file named backup(yyyy-mm-dd) in your current folder.

Urldecode in batch file

How can I 'urldecode' the following string in a batch file? I need to change the following
http://www.example.com/some-page/some/link.html
to this
http://www.example.com/some-page/some/link.html
It can be done in Javascript with decodeURIComponent() but I've no idea how to do it in a batch file just within MSDOS etc.
It can be done in Javascript with decodeURIComponent() but I've no idea how to do it in a batch file just within MSDOS etc.
Not with decodeURIComponent(), no. Be that as it may, the solution still lies with invoking JavaScript. You can call on the IE7 JavaScript engine via JScript. See this page for more examples of batch + JScript hybrid scripts.
There are no built-in JavaScript functions to convert HTML entities (the ΓΏ characters) back to plain text, but it's not too difficult to roll your own.
#if (#CodeSection == #Batch) #then
:: batch portion
#echo off
setlocal
set "url=http://www.example.com/some-page/some/link.html"
cscript /nologo /e:JScript "%~f0" "%url%"
goto :EOF
#end
// JScript portion
function decodeEntities(what) {
return what.replace(/&#x([0-9a-f]{2});/ig, function(m, $1) {
return String.fromCharCode(parseInt($1, 16));
});
}
WSH.Echo(decodeEntities(WSH.Arguments(0)));
Just as an academic exercise, here's a pure batch method of converting HTML character entities to ASCII. It's slightly slower than the JScript hybrid, and it'll probably mistreat strings containing exclamation marks.
#echo off
setlocal enabledelayedexpansion
set "url=http://www.example.com/some-page/some/link.html"
set "url=%url:&#x=;0x%"
:begin
for %%I in (%url%) do (
set "chunk=%%~I"
if "!chunk:~0,2!"=="0x" if "!chunk:~4,1!"=="" (
for /f "delims=" %%x in (
'forfiles /m "%~nx0" /c "cmd /c echo(%%~I"'
) do set "url=!url:;%%I;=%%~x!"
goto begin
)
)
)
echo !url!
This may be achieved in pure Batch...
#echo off
setlocal EnableDelayedExpansion
set "input=http://www.example.com/some-page/some/link.html"
rem Define the equivalences
for %%a in ("#x3a=:" "#x2f=/") do (
for /F "tokens=1,2 delims==" %%b in (%%a) do set "replace[%%b]=%%c"
)
echo Input = "%input%"
set "input=%input:&=\%"
set "output="
for %%a in (%input%) do (
for /F "tokens=1,2 delims=\" %%b in ("%%a") do (
if "%%c" neq "" (
set "output=!output!%%b!replace[%%c]!"
) else (
set "term=%%a"
if "!term:~0,1!" equ "\" (
set "output=!output!!replace[%%b]!"
) else (
set "output=!output!%%b"
)
)
)
)
echo Output = "%output%"
#if (#x)==(#y) #end /***** jscript comment ******
#echo off
set "url=http://www.example.com/some-page/some/link.html"
cscript //E:JScript //nologo "%~f0" "%url%"
exit /b 0
#if (#x)==(#y) #end ****** end comment *********/
var args=WScript.Arguments;
//WScript.Echo(args.Item(0));
WScript.Echo(decodeURIComponent(args.Item(0)));
though this definitely is not uri encoded url...

How do I create a batch file that reads a file and writes a substring into another file?

I currently have an exported text file (output.txt) from a Clear-Case command that I need to parse. It looks like this:
Comparing the following:
R1.PROD.V1#\VOB_pvob
R2.PROD.V1#\VOB_pvob
Differences:
>> M:\ACME_PROD\src\ACME##
>> M:\ACME_PROD\src\ACME\file 2.txt##
>> M:\ACME_PROD\src\ACME\file 1.txt##
>> M:\ACME_PROD\src\ACME\file 3.txt##
What I would like to do is use the findstr command to filter the strings that are contained between the ">> " and "##" strings to get an output file that looks like this (with quotes if possible:
"M:\ACME_PROD\src\ACME"
"M:\ACME_PROD\src\ACME\file 2.txt"
"M:\ACME_PROD\src\ACME\file 1.txt"
"M:\ACME_PROD\src\ACME\file 3.txt"
I am new to writing batch files and so I don't exactly know where to start. I have managed to find code that can loop through the lines of a text file and separate code for the findstr command, but I get stuck trying to put it all together!
Best regards,
Andrew
Here you go
setlocal enabledelayedexpansion
for /f "skip=1 tokens=* delims=> " %%a in ('"findstr /r [\w^>*] output.txt"') do (
set line=%%a
set line=!line:#=!
echo "!line!" >>new.txt
)
The filtered strings will be outputted into new.txt.

Batch : getting the last folder name from a absolute path

I am using a Batch script to automatically backup files to my NAS and I need to get the last folder name from an absolute path, like from "C:\Things\Folder" to "Folder"
It's a bit of a hack, but you can use:
Set NasPath=C:\Things\Folder
Set NasFolder=%NasPath%
:GetFolder
Set GetFolderTemp=%NasFolder:*\=%
If Not %GetFolderTemp%==%NasFolder% (
Set NasFolder=%GetFolderTemp%
Goto :GetFolder
)
Echo NasPath =%NasPath%
Echo NasFolder=%NasFolder%
Exit /B
Whatever you do, don't put quotes around any part of the Set NasPath=... statement. Use quotes this way:
Set FromPath=C:\Program Files\Blah
Set NasPath=C:\Things\Folder
RoboCopy "%FromPath%" "%NasPath%"
Do not use quotes this way:
Set FromPath="C:\Program Files\Blah"
Set NasPath="C:\Things\Folder"
RoboCopy %FromPath% %NasPath%
To not have any issue with space, I propose this code:
Set NasPath=C:\Things\My Space\Folder
Set GetFolderTemp=%NasPath%
:GetFolder
FOR /F "tokens=1,* delims=\" %%1 IN ("%GetFolderTemp%") do (
set NasFolder=%%1
set GetFolderTemp=%%2
)
if not "a%GetFolderTemp%"=="a" goto :GetFolder
echo %NasFolder%
Assuming C:\Program Files\Mickey\Mouse-like paths (without quotes), you could also use the following code:
setlocal EnableDelayedExpansion
set path=C:\Program Files\Microsoft\Mickey\Mouse
:shift
for /f "tokens=1* delims=\/" %%i in ( "!path!" ) do (
set folder=%%i
set path=%%j
)
if not [!path!] == [] goto :shift
echo folder: !folder!
endlocal

How to get folder path from file path with CMD

I need path to the folder that contains cmd file.
With %0 I can get the file name. But how to get the folder name?
c:\temp\test.cmd >> test.cmd
P.S. My current directory != folder of the script.
For the folder name and drive, you can use:
echo %~dp0
You can get a lot more information using different modifiers:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
This is a copy paste from the "for /?" command on the prompt.
Related
Top 10 DOS Batch tips (Yes, DOS Batch...) shows batchparams.bat (link to source as a gist):
C:\Temp>batchparams.bat c:\windows\notepad.exe
%~1 = c:\windows\notepad.exe
%~f1 = c:\WINDOWS\NOTEPAD.EXE
%~d1 = c:
%~p1 = \WINDOWS\
%~n1 = NOTEPAD
%~x1 = .EXE
%~s1 = c:\WINDOWS\NOTEPAD.EXE
%~a1 = --a------
%~t1 = 08/25/2005 01:50 AM
%~z1 = 17920
%~$PATHATH:1 =
%~dp1 = c:\WINDOWS\
%~nx1 = NOTEPAD.EXE
%~dp$PATH:1 = c:\WINDOWS\
%~ftza1 = --a------ 08/25/2005 01:50 AM 17920 c:\WINDOWS\NOTEPAD.EXE
The accepted answer is helpful, but it isn't immediately obvious how to retrieve a filename from a path if you are NOT using passed in values. I was able to work this out from this thread, but in case others aren't so lucky, here is how it is done:
#echo off
setlocal enabledelayedexpansion enableextensions
set myPath=C:\Somewhere\Somewhere\SomeFile.txt
call :file_name_from_path result !myPath!
echo %result%
goto :eof
:file_name_from_path <resultVar> <pathVar>
(
set "%~1=%~nx2"
exit /b
)
:eof
endlocal
Now the :file_name_from_path function can be used anywhere to retrieve the value, not just for passed in arguments. This can be extremely helpful if the arguments can be passed into the file in an indeterminate order or the path isn't passed into the file at all.
In order to assign these to variables, be sure not to add spaces in front or after the equals sign:
set filepath=%~dp1
set filename=%~nx1
Then you should have no issues.
In case anyone wants an alternative method...
If it is the last subdirectory in the path, you can use this one-liner:
cd "c:\directory\subdirectory\filename.exe\..\.." && dir /ad /b /s
This would return the following:
c:\directory\subdirectory
The .... drops back to the previous directory.
/ad shows only directories
/b is a bare format listing
/s includes all subdirectories. This is used to get the full path of the directory to print.
I had same problem in my loop where i wanted to extract zip files in the same directory and then delete the zip file. The problem was that 7z requires the output folder, so i had to obtain the folder path of each file. Here is my solution:
FOR /F "usebackq tokens=1" %%i IN (`DIR /S/B *.zip` ) DO (
7z.exe x %%i -aoa -o%%i\..
)
%%i was a full filename path and %ii\.. simply returns the parent folder.
hope it helps.
In case the accepted answer by Wadih didn't work for you, try echo %CD%
This was put together with some edited example cmd
#Echo off
Echo ********************************************************
Echo * ZIP Folder Backup using 7Zip *
Echo * Usage: Source Folder, Destination Drive Letter *
Echo * Source Folder will be Zipped to Destination\Backups *
Echo ********************************************************
Echo off
set year=%date:~-4,4%
set month=%date:~-10,2%
set day=%date:~-7,2%
set hour=%time:~-11,2%
set hour=%hour: =0%
set min=%time:~-8,2%
SET /P src=Source Folder to Backup:
SET source=%src%\*
call :file_name_from_path nam %src%
SET /P destination=Backup Drive Letter:
set zipfilename=%nam%.%year%.%month%.%day%.%hour%%min%.zip
set dest="%destination%:\Backups\%zipfilename%"
set AppExePath="%ProgramFiles(x86)%\7-Zip\7z.exe"
if not exist %AppExePath% set AppExePath="%ProgramFiles%\7-Zip\7z.exe"
if not exist %AppExePath% goto notInstalled
echo Backing up %source% to %dest%
%AppExePath% a -r -tzip %dest% %source%
echo %source% backed up to %dest% is complete!
TIMEOUT 5
exit;
:file_name_from_path <resultVar> <pathVar>
(
set "%~1=%~nx2"
exit /b
)
:notInstalled
echo Can not find 7-Zip, please install it from:
echo http://7-zip.org/
:end
PAUSE
IMHO the simplest yet most powerful method to get the full path of a file is:
Start Notepad
Copy and Paste the following text:
#echo %1 | clip 3
Save the file as "CopyAsPath.bat"
Now you can either
drag a file over that batch
or
copy it into your SendTo menu folder (%USERPROFILE%\AppData\Roaming\Microsoft\Windows\SendTo)
Whether you follow the option 1 or the option 2, one millisecond later you can either
"press Ctrl-V"
or
use "Right Mouse Click -> Paste"
to paste the full path+filename wherever you want.
Simple, powerful, and without using any external Windows tool.

Resources