Exchange parts of strings in a text file - windows

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

Related

batch, dealing with spaces in path

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"
)

Windows Batch scripting(splitting of strings using multiple delimiters)

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
)

Reading list of files in a directory and copying the contents using batch command file

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!"
)

Result of each repeat of the loop to different file WIN BATCH

I would like to resolve simple problem - Saving to different file when loop repeats. I know im getting results because when I am doing >>file.txt
i am getting all restults into one file. It would be GREAT to save results to different files each time(and name this file by text from variable. but there is something wrong. It saves the results of last loop iteration.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=1* delims=;" %%A in (list2.csv) do (
SET /A vidx=!vidx! + 1
set var!vidx!=%%A
rxrepl -f temp.txt -s "xNAMEx" -r "%%A">file___%var!vidx!%.txt
)
Try replacing this
file___%var!vidx!%.txt by this file___!var!!vidx!!!.txt
You don't need any variables to get the result you are looking for. Also, the * is usesless in "tokens=1*" if you never reference variable %%B. And "tokens=1" is the default. So all you need is:
for /F "delims=;" %%A in (list2.csv) do rxrepl -f temp.txt -s "xNAMEx" -r "%%A">"file___%%A.txt"
If you really want to build an "array" of var.N values, you can use FINDSTR to prefix each line with an incrementing number (line number).
for /F "tokens=1,2 delims=:;" %%A in ('findstr /n "^" list2.csv') do (
set "var.%%A=%%B"
set "var.cnt=%%A"
rxrepl -f temp.txt -s "xNAMEx" -r "%%B">"file___%%B.txt"
)
:: Display the "array" values
for /l %%N in (1 1 %var.cnt%) do echo var.%%N=!var.%%N!
Use just ...>"file___%%~A.txt" instead of erroneous >file___%var!vidx!%.txt
#ECHO OFF
SETLOCAL enableextensions enabledelayedexpansion
set vidx=0
for /F "tokens=1* delims=;" %%A in (list2.csv) do (
SET /A "vidx+=1"
set "var!vidx!=%%~A"
echo loopvar %%%% A=%%~A "file___%%~A.txt"
rem next line shows how to treat array-like names
call set "filenamepart=%%var!vidx!%%"
echo filenamepart=!filenamepart! "file___!filenamepart!.txt"
rem rxrepl -f temp.txt -s "xNAMEx" -r "%%A">"file___%%~A.txt"
)

Replace string % with %% using batch file

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

Resources