If a.txt contains
a b c
abc
The command for /f %x in (a.txt) do echo %x is printing
a
abc
What am I doing wrong?
for /f "tokens=*" %x in (a.txt) do #echo %x
The #echo will prevent the echo line from being printed
for /f "delims=|" %i in (a.txt) do #echo %i
Inside "delims=|" you can use any character for the delimiter that is not part of the file
#echo off
setlocal
for /F "usebackq tokens=1-3 delims= " %%a IN ("a.txt") DO (
if not "%%a"=="" echo.%%a
if not "%%b"=="" echo.%%b
if not "%%c"=="" echo.%%c
)
Tokens - the set of chars delimited by one of the delim char.
You can specify many delim chars, i.e. delims= .,
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 have to parse the following
/*-secure- {"challenges":{"wl_antiXSRFRealm":{"WL-Instance-Id":"3mva6l4ph7816571thcnc391vj"}}}*/
and extract 3mva6l4ph7816571thcnc391vj
for /f usebackq^ tokens^=8^ delims^=^" %%a in ("response.txt") do echo %%a
Use the quote as delimiter and get the required field
#echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in ('find /i "child2" test.txt') do (
set a=%%i
set a=!a:*child2":"=!
set a=!a:"}}}*/=!
)
echo %a%
set a=!a:*child2":"=! removes all from beginning of the line including child2":"
set a=!a:"}}}*/=! removes the string "}}}*/ at the end
With a cleaned input like this:
{"challenges":{"wl_antiXSRFRealm":{"WL-Instance-Id":"3mva6l4ph7816571thcnc391vj"}}}
This should work:
jq -r .challenges.wl_antiXSRFRealm."WL-Instance-Id" response.txt
Output:
3mva6l4ph7816571thcnc391vj
What I am trying to do is add quotation marks around the tenth token of a text files so a number would read "55555" instead of 55555. While leaving the other tokens untouched.
What I have as of now which doesn't work is:
FOR /F "skip=1 delims=, tokens=*" %i in (file.txt) do
IF tokens EQU 10
echo "%i" >> data.txt
ELSE
echo %i >> data.txt
FOR /F "skip=1tokens=1-9,10*delims=," %i in (file.txt) do (
>>data.txt echo(%%i,%%j,%%k,%%l,%%m,%%n,%%o,%%p,%%q,"%%r",%%s
)
should work for you.
I have a text like below in input.txt file.
sdf%5Ddfssdsd%2Ddfdf
I would like to replace "%" with "%%" in the output file. so the text should look like
sdf%%5Ddfssdsd%%2Ddfdf
Appreciate your help!!
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1* delims=" %%i in (input.txt) do (
set _line=%%i
set _line=!_line:%%=%%%%!
echo !_line! >> output.txt
)
endlocal