I have a property file (test.properties) which has a variable which holds multiple strings.
Ex: var=str1;str2;str3;.....
I need to use the above properties file in my batch file (abc.bat), so that i could print the values line by line. Output of the batch file should look like this...
str1
str2
str3
...
...
(and so on)
Any help could be appreciated..Thanx:)
Ive tried to use "for loop" to seperate the values from first delimiter(=) in this way...
IF EXIST "test.properties"
(
ECHO test.properties file found
for /F "tokens=1,2 delims==" %%A IN (test.properties) DO
(
set value="%%B"
ECHO !value!
)
)
Output=str1;str2;str3;....
Now if i want to parse the strings in "!value!" line by line i use ...
for /F "tokens=* delims=;" %%x IN ("!value!") DO
(
ECHO %%x
)
I am facing error.....Any help?
just use a plain for to get elements of a list (; is a standard delimiter)
#echo off
setlocal enabledelayedexpansion
>test.properties echo var=str1;str2;str3;str4;str5;str6
IF EXIST "test.properties" (
ECHO test.properties file found
for /F "tokens=1,2 delims==" %%A IN (test.properties) DO (
set "value=%%B"
ECHO !value!
)
for %%x IN (!value!) DO echo %%x
)
Related
I have a CSV file with leading-and-trailing doublequotes per line I want to remove, and made a DOS batch to do it. The following works for an explicit path:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=*" %%A in (C:\Folder\WrappedInQuotes.csv) do (
set line=%%A
echo !line:~1,-1! >> C:\Folder\UnWrapped.csv
)
Of course, if the path has spaces in it, the following will not work:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=*" %%A in (C:\Folder\Sub Folder\WrappedInQuotes.csv) do (
set line=%%A
echo !line:~1,-1! >> C:\Folder\Sub Folder\UnWrapped.csv
)
(#echo on, the message is "...cannot find the file C:\Folder\Sub", of course)
As a next-step test, I simply wrapped the two explicit filespecs in doublequotes:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=*" %%A in ("C:\Folder\Sub Folder\WrappedInQuotes.csv") do (
set line=%%A
echo !line:~1,-1! >> "C:\Folder\Sub Folder\UnWrapped.csv"
)
With #echo on, the For seems to be getting the correct filespec (original CSV), but now the destination CSV has
:\Folder\Sub Folder\EachLineWrappedInQuotes.cs
(the source CSV full filespec, with first and last characters removed), instead of the contents of the source CSV with first and last characters (the doublequote wrapping) removed.
Ultimately, I want to replace the explicit paths with a path variable like %~dp0, but haven't been able to get past the "next-step test".
(I have tried to solve this by studying the many answers already given, with no success, sorry!)
To get the content of the file and remove double quotes, without the need to set variables, set usebackq
method 1:
#echo off
for /F "usebackq tokens=*" %%A in ("C:\Folder\Sub Folder\WrappedInQuotes.csv") do (
echo %%~A >> "C:\Folder\Sub Folder\UnWrapped.csv"
)
method 2, if you still want to set the variable:
#echo off
setlocal enabledelayedexpansion
for /F "usebackq tokens=*" %%A in ("C:\Folder\Sub Folder\WrappedInQuotes.csv") do (
set line=%%~A
echo !line! >> "C:\Folder\Sub Folder\UnWrapped.csv"
)
Or by using type :
#echo off
for /F "tokens=*" %%A in ('type "C:\Folder\Sub Folder\WrappedInQuotes.csv"') do (
echo %%~A >> "C:\Folder\Sub Folder\UnWrapped.csv"
)
I have a list of csv files in a directory which have name with format XX_YYYFile.csv, where XX is a name that can have any characters (including space), and YYY is random 3 digits. For example: "book_123File.csv", "best movie_234File.csv", etc. I want to read this list of files then create new CSV files by removing "_YYYFile". The content of the new files are the same with the original ones, except the first line needs to be added with value "number,name,date".
set inputFileFolder=C:\Input
set outputFileFolder=C:\Output
FOR /F "delims=" %%F IN ('DIR %inputFileFolder%\*File.csv /B /O:D') DO (
set reportInputFile=%inputFileFolder%\%%F
set reportInputFileName=%%F
set result=!reportInputFileName:~0,-12!
set reportOutputFileName=!result!.csv
set reportOutputFile=%outputFileFolder%\!result!.csv
echo number,name,date > !reportOutputFile!
for /f "tokens=* delims=" %%a in (!reportInputFile!) do (
echo %%a >> !reportOutputFile!
)
)
If I run this batch file, file "book.csv" is successfully created with the correct contents (first line: "number,name,date", the next lines are from file "book_123.csv"). But file "best movie_234.csv" and other files contain space in the filename are not created successfully. File "best movie.csv" is created with only 1 line "number,name,date". The contents of file "best movie_234.csv" are not copied to file "best movie.csv".
Please help.
You need to Escape Characters, Delimiters and Quotes properly. Note the usebackq parameter in inner for /F loop as well:
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "inputFileFolder=C:\Input"
set "outputFileFolder=C:\Output"
FOR /F "delims=" %%F IN ('DIR "%inputFileFolder%\*File.csv" /B /O:D') DO (
set "reportInputFile=%inputFileFolder%\%%F"
set "reportInputFileName=%%F"
set "result=!reportInputFileName:~0,-12!"
set "reportOutputFileName=!result!.csv"
set "reportOutputFile=%outputFileFolder%\!result!.csv"
>"!reportOutputFile!" echo number,name,date
for /f "usebackq tokens=* delims=" %%a in ("!reportInputFile!") do (
>>"!reportOutputFile!" echo %%a
)
rem above `for /f ... %%a ...` loop might be replaced by FINDSTR
rem >>"!reportOutputFile!" findstr "^" "!reportInputFile!"
rem or by TYPE
rem >>"!reportOutputFile!" type "!reportInputFile!"
)
Hint: each > and >> redirector works as follows:
opens specified oputput file, then
writes something to oputput file, and finally
closes oputput file.
This procedure might be extremely slow if repeated in next for /f ... %%a ... loop for larger files:
>"!reportOutputFile!" echo number,name,date
for /f "usebackq tokens=* delims=" %%a in ("!reportInputFile!") do (
>>"!reportOutputFile!" echo %%a
)
Use block syntax rather:
>"!reportOutputFile!" (
echo number,name,date
for /f "usebackq tokens=* delims=" %%a in ("!reportInputFile!") do (
echo %%a
)
)
above for /f ... %%a ... loop might be replaced by FINDSTR command (it eliminates empty lines like for does) as follows:
>"!reportOutputFile!" (
echo number,name,date
findstr "^." "!reportInputFile!"
)
or by TYPE command (it will retain empty lines unlike for) as follows:
>"!reportOutputFile!" (
echo number,name,date
type "!reportInputFile!"
)
I am parsing an xml file its format is like:
<ResourcePicture Name="a.jpg">
<GeneratedPicture Name="b.jpg"/>
<GeneratedPicture Name="c.jpg"/>
</ResourcePicture>
I can print each name of the pictures but I want to check something with an if statement inside there. However I get an error "< was unexpected at this time." but when I remove the if statement, that works just as fine. So what am I missing? Thanks in advance.
set "xmlFile=pictures.xml"
REM split xml file into lines
for /f "tokens=* skip=2" %%a in (%xmlFile%) do (
REM %%a contais a string like <ResourcePicture Name="a.jpg">
set "currnetLine=%%a"
set "currnetLine=!currnetLine:"=+!"
set counter=0
for /f "delims=+ tokens=2" %%c in ("!currnetLine!") do (
REM %%c contains the picture name like a.jpg
set /a counter=counter+1
REM CHECKING SOMETHING
if not %counter%==1 ( echo abc )
echo %%c
)
)
you need delayed expansion (I dont know if the logic of the script achieves what you want but like this it has no errors):
#echo off
set "xmlFile=pictures.xml"
REM split xml file into lines
setlocal enableDelayedExpansion
for /f "tokens=* skip=2" %%a in (%xmlFile%) do (
REM %%a contais a string like <ResourcePicture Name="a.jpg">
set "currnetLine=%%a"
set "currnetLine=!currnetLine:"=+!"
set counter=0
for /f "delims=+ tokens=2" %%c in ("!currnetLine!") do (
REM %%c contains the picture name like a.jpg
set /a counter=counter+1
REM CHECKING SOMETHING
if not !counter! == 1 ( echo abc )
echo %%c
)
)
I'm trying to translate a .bat file into a .sh script. Several parameters are passed to the script, one of these being a hash table. The code looks like...
date /T
time /T
FOR /F "tokens=1-11" %%A IN (%4) DO (
set args1=%%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K
)
FOR /F "tokens=12" %%A IN ("%4") DO (
set args2=%%A
)
FOR /F "tokens=12*" %%A IN (%4) DO (
set dummy=%%A
set args3=%%B
)
I'm not sure what is going on here, or how to handle it?
Any suggestions? Or good reference pages online I can take at look at?
Here is a good reference page: http://technet.microsoft.com/en-us/library/bb490909.aspx
Breakdown
The first loop is treating the input as a filenameset.
This is storing the first 11 whitespace delimited items in the variable args1.
The second loop is treating the input as a literal string.
This is storing just the 12 whitespace delimited item in the variable args2.
The last loop is treating the input as a filenameset.
This is storing all the remaining whitespace delimited items after the 12th item in the variable args3.
Example
I would recommend adding the echo command after each loop so you can see what the parsed values look like.
FOR /F "tokens=1-11" %%A IN (%4) DO (
set args1=%%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K
)
echo %args1%
FOR /F "tokens=12" %%A IN ("%4") DO (
set args2=%%A
)
echo %args2%
FOR /F "tokens=12,*" %%A IN (%4) DO (
set args3=%%B
)
echo %args3%
is there a way to transfer characters in the a plain .txt file.
I have a lot of entries in the .txt file and they are all in this format:
1 = Example,
2 = Example2,
3 = Example3...
what I need is a batch file to transfer "# =" to the right side of the string. So it looks like this:
Example = 1,
Example2 = 2...
Is it possible to that with a .bat file?
This will handle more than one word where you have Example...
#echo off
for /f "tokens=1,* delims== " %%a in (input.txt) do >>output.txt echo %%b = %%a
If you can use GNU sed:
sed "s/\(\S\+\)\s=\s\(\S\+\)/\2 = \1/" file
Something like this should work:
#echo off
pushd "C:\some\where"
for /f "tokens=1,2 delims==" %%a in (input.txt) do echo %%b = %%a
popd
Or use this to get rid of whitespace:
#echo off
setlocal EnableDelayedExpansion
pushd "C:\some\where"
for /f "tokens=1,2 delims==" %%a in (input.txt) do (
set "key=%%a"
set "val=%%b"
echo !val: =! = !key: =!
)
popd
To save the output to a file, call either script like this:
switch.cmd >output.txt