batch, dealing with spaces in path - windows

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

Related

Batch String +=? [duplicate]

I made this code
dir /B /S %RepToRead% > %FileName%
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo %z%
echo %%a
)
echo %%a is working fine but echo %z% returns "echo disabled".
I need to set a %z% because I want to split the variable like %z:~7%
Any ideas?
There are two methods to setting and using variables within for loops and parentheses scope.
setlocal enabledelayedexpansion see setlocal /? for help. This only works on XP/2000 or newer versions of Windows.
then use !variable! instead of %variable% inside the loop...
Create a batch function using batch goto labels :Label.
Example:
for /F "tokens=*" %%a in ('type %FileName%') do call :Foo %%a
goto End
:Foo
set z=%1
echo %z%
echo %1
goto :eof
:End
Batch functions are very useful mechanism.
You probably want SETLOCAL ENABLEDELAYEDEXPANSION. See https://devblogs.microsoft.com/oldnewthing/20060823-00/?p=29993 for details.
Basically: Normal %variables% are expanded right aftercmd.exe reads the command. In your case the "command" is the whole
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo %z%
echo %%a
)
loop. At that point z has no value yet, so echo %z% turns into echo. Then the loop is executed and z is set, but its value isn't used anymore.
SETLOCAL ENABLEDELAYEDEXPANSION enables an additional syntax, !variable!. This also expands variables but it only does so right before each (sub-)command is executed.
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo !z!
echo %%a
)
This gives you the current value of z each time the echo runs.
I struggeld for many hours on this.
This is my loop to register command line vars.
Example : Register.bat /param1:value1 /param2:value2
What is does, is loop all the commandline params,
and that set the variable with the proper name to the value.
After that, you can just use
set value=!param1!
set value2=!param2!
regardless the sequence the params are given. (so called named parameters).
Note the !<>!, instead of the %<>%.
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%P IN (%*) DO (
call :processParam %%P
)
goto:End
:processParam [%1 - param]
#echo "processparam : %1"
FOR /F "tokens=1,2 delims=:" %%G IN ("%1") DO (
#echo a,b %%G %%H
set nameWithSlash=%%G
set name=!nameWithSlash:~1!
#echo n=!name!
set value=%%H
set !name!=!value!
)
goto :eof
:End
Simple example of batch code using %var%, !var!, and %%.
In this example code, focus here is that we want to capture a start time using the built in variable TIME (using time because it always changes automatically):
Code:
#echo off
setlocal enabledelayedexpansion
SET "SERVICES_LIST=MMS ARSM MMS2"
SET START=%TIME%
SET "LAST_SERVICE="
for %%A in (%SERVICES_LIST%) do (
SET START=!TIME!
CALL :SOME_FUNCTION %%A
SET "LAST_SERVICE=%%A"
ping -n 5 127.0.0.1 > NUL
SET OTHER=!START!
if !OTHER! EQU !START! (
echo !OTHER! is equal to !START! as expected
) ELSE (
echo NOTHING
)
)
ECHO Last service run was %LAST_SERVICE%
:: Function declared like this
:SOME_FUNCTION
echo Running: %1
EXIT /B 0
Comments on code:
Use enabledelayedexpansion
The first three SET lines are typical
uses of the SET command, use this most of the time.
The next line is a for loop, must use %%A for iteration, then %%B if a loop inside it
etc.. You can not use long variable names.
To access a changed variable such as the time variable, you must use !! or set with !! (have enableddelayexpansion enabled).
When looping in for loop each iteration is accessed as the %%A variable.
The code in the for loop is point out the various ways to set a variable. Looking at 'SET OTHER=!START!', if you were to change to SET OTHER=%START% you will see why !! is needed. (hint: you will see NOTHING) output.
In short !! is more likely needed inside of loops, %var% in general, %% always a for loop.
Further reading
Use the following links to determine why in more detail:
Difference between %variable% and !variable! in batch file
Variable usage in batch file
To expand on the answer I came here to get a better understanding so I wrote this that can explain it and helped me too.
It has the setlocal DisableDelayedExpansion in there so you can locally set this as you wish between the setlocal EnableDelayedExpansion and it.
#echo off
title %~nx0
for /f "tokens=*" %%A in ("Some Thing") do (
setlocal EnableDelayedExpansion
set z=%%A
echo !z! Echoing the assigned variable in setlocal scope.
echo %%A Echoing the variable in local scope.
setlocal DisableDelayedExpansion
echo !z! &rem !z! Neither of these now work, which makes sense.
echo %z% &rem ECHO is off. Neither of these now work, which makes sense.
echo %%A Echoing the variable in its local scope, will always work.
)
set list = a1-2019 a3-2018 a4-2017
setlocal enabledelayedexpansion
set backup=
set bb1=
for /d %%d in (%list%) do (
set td=%%d
set x=!td!
set y=!td!
set y=!y:~-4!
if !y! gtr !bb1! (
set bb1=!y!
set backup=!x!
)
)
rem: backup will be 2019
echo %backup%
Try this:
setlocal EnableDelayedExpansion
...
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo !z!
echo %%a
)
You can use a macro if you access a variable outside the scope
#echo off
::Define macro
set "sset=set"
for /l %%a in (1,1,4) do (
::set in loop
%sset% /a "x[%%a]=%%a*%%a"
if %%a equ 4 (
:: set in condition
%sset% "x[%%a]=x Condition"
%sset% "y=y Condition"
)
)
echo x1=%x[1]% x2=%x[2]% x3=%x[3]% x4=%x[4]% y=%y%
:: Bonus. enableDelayedExpansion used to access massive from the loop
setlocal enableDelayedExpansion
echo Echo from the loop
for /l %%a in (1,1,4) do (
::echo in one line - echo|set /p =
echo|set /p "=x%%a=!x[%%a]! "
if %%a equ 4 echo y=%y%
)
pause
I know this isn't what's asked but I benefited from this method, when trying to set a variable within a "loop". Uses an array. Alternative implementation option.
SETLOCAL ENABLEDELAYEDEXPANSION
...
set Services[0]=SERVICE1
set Services[1]=SERVICE2
set Services[2]=SERVICE3
set "i=0"
:ServicesLoop
if defined Services[%i%] (
set SERVICE=!Services[%i%]!
echo CurrentService: !SERVICE!
set /a "i+=1"
GOTO :ServicesLoop
)
The following should work:
setlocal EnableDelayedExpansion
for /F "tokens=*" %%a in ('type %FileName%') do (
set "z=%%a"
echo %z%
echo %%a
)

Batch file reading dynamic values from properties file and inject values to those values from outside the batch file

I am trying to inject values to a dynamic values I received from outside a batch script.
To be more clear here is an example:
I have a property file named prop.properties that contain 2 values:
prop.properties
path1=%value1%/...
path2=%value1%/...
in addition I have a batch myFile.bat file that read the property file.
myFile.bat
echo off
set value1=%1
For /F "tokens=1* delims==" %%A IN (prop.properties) DO (
IF "%%A"=="path1" set val1=%%B
IF "%%A"=="path2" set val2=%%B
)
echo %var1%
echo %var2%
so after running myFile.bat C:
I will get in the output:
C:/...
c:/...
or if I run:
myFile.bat D:
I will get:
D:/...
D:/...
If I understand what you're trying to do then use delayed variable expansion:
Change percent to exclamation point in prop.proerties
path1=!value1!/...
path2=!value1!/...
then:
#echo off
setlocal EnableDelayedExpansion
set value1=%1
For /F "tokens=1* delims==" %%A IN (prop.properties) DO (
IF "%%A"=="path1" set var1=%%B
IF "%%A"=="path2" set var2=%%B
)
echo %var1%
echo %var2%
Though there are so many other things/questions.
Here's one idea:
#echo off
setlocal EnableDelayedExpansion
set value1=%1
For /F "tokens=1* delims==" %%A IN (prop.properties) DO (
set var_%%A=%%B
)
echo %var_path1%
echo %var_path2%
set var_
for /f "tokens=2 delims==" %%a in ('set var_') do echo %%a
Though I don't know why you don't just:
prop.properties
path1=/...
path2=/...
#echo off
for /F "tokens=1* delims==" %%A IN (prop.properties) DO echo %value1%%%A

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

Batch - If statement causes an error

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

Getting a list of common strings with first occurance mark

I've got bunch of text files with some content. First I wanted to number the lines globally. Then I extracted all lines that are duplicated somewhere (occur in any of given files at least twice). But now I need to mark all of these lines with the filename and line number of the first occurrence of this line. And now the funny part - it needs to be a windows batch file, using native windows tools. That's why I've got this problem to begin with.
So, to sum it up:
I have a file A with unique strings/lines, each of them is said to occur at least twice in given set of files.
I need to search these files and mark all occurrences of given line from A file with
-file name in which the line first occured
-line number in this file
This is my code with effort to number lines and format files.
#echo off
setlocal EnableDelayedExpansion
set /a lnum=0
if not [%1]==[] pushd %1
for /r %%F in (*.txt) do call :sub "%%F"
echo Total lines in %Files% files: %Total%
popd
exit /b 0
:Sub
set /a Cnt=0
for /f %%n in ('type %1') do (
set /a Cnt+=1
set /a lnum=!lnum!+1
echo ^<!lnum!^> %%n >> %1_ln.txt && echo ^<!lnum!^> >> %1_ln.txt && echo. >> %1_ln.txt
)
set /a Total+=Cnt
set /a Files+=1
echo %1: %Cnt% lines
#echo off
setlocal EnableDelayedExpansion
set lnum=0
if not "%~1" == "" pushd %1
rem "I've got bunch of text files..." (%%F is file name)
for /r %%F in (*.txt) do call :sub "%%F"
echo Total lines in %Files% files: %lnum%
popd
exit /b 0
:Sub "filename"
set Cnt=0
rem "... with some content." (%%n is line contents)
(for /f "usebackq delims=" %%n in (%1) do (
set /a Cnt+=1
rem "First I wanted to number the lines globally."
set /a lnum+=1
echo ^<!lnum!^> %%n
rem "Then I extracted all lines that are duplicated somewhere" (that were defined before)
if defined line[%%n] (
rem "I need to mark all of these lines with the filename and line number of the first occurrence of this line."
echo ^<!line[%%n]!^>
echo/
) else (
REM (Store the first occurrence of this line with *local* line number and filename)
set line[%%n]=!Cnt!: %1
)
)) > "%~PN1_ln.txt"
set /A Files+=1
echo %1: %Cnt% lines
exit /B
The above Batch program ignore empty lines in the input files and fail if they contain special Batch characters, like ! & < > |; this limitation may be fixed if required.
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%s IN (A) DO (
SET searching=Y
FOR /f "delims=" %%f IN (
'dir /s /b /a-d *.txt') DO IF DEFINED searching (
FOR /f "tokens=1delims=:" %%L IN (
'findstr /b /e /n /l /c:"%%s" ^<"%%f"') DO IF DEFINED searching (
ECHO Line %%L IN "%%f" FOUND "%%s"
SET "searching="
)
)
)
Here's the meat of a routine that should do what you appear to be looking for - and that's as clear as mud.
It looks through the "A" file for each string in turn, assigns the string to %%s and sets the flag searching
Then it looks through the file list, assigning filenames to %%f
Then it executes a findstr to find the /c:"%%s" complete string %%s (including any spaces) in /l or literal mode (ie. not using regular expressions) for a line that both /b and /e begins and ends with the target (ie exactly matches) and /n numbers those lines.
The output of findstr will be in the format linenumber:linecontents so if this line is examined by the FORwith the option "delims=:" then the partion up to the first : is assigned to to %%L
So - %%L contains the line#, %%f the filename, %%s the string
Clearing searching having detected this line by setting its value to [nothing] means it's not NOT DEFINED hence no further lines will be reported from the current file, and no further filenames will be examined.
Now if you want to get a listing of ALL of the occurrences of the target lines, all you need to do is to REM-out the SET "searching=" line. Searching will then never be reset, so each line in each file is reported.
If you want some other combination, please clarify.
I have absolutely no idea whatever what you mean by "marking" a line.
#ECHO OFF & setlocal
for /f "tokens=1*delims==" %%i in ('set "$" 2^>nul') do set "%%i="
for %%a in (*.txt) do (
for /f %%b in ('find /v /c "" ^<"%%a"') do echo(%%b lines in %%a.
set /a counter=0, files+=1
for /f "usebackqdelims=" %%b in ("%%~a") do (
set /a counter+=1, total+=1
set "line=%%b"
setlocal enabledelayedexpansion
if not defined $!line! set "$!line!=%%a=!counter!=!line!"
for /f "delims=" %%i in ('set "$" 2^>nul') do (if "!"=="" endlocal)& set "%%i"
)
)
echo(%total% lines in %files% files.
for /f "delims=" %%a in (a) do set "#%%a=%%a"
for /f "tokens=2,3*delims==:" %%i in ('set "$" 2^>nul') do (
if defined #%%k echo("%%k" found in %%i at line %%j.
)
Script can handle !&<>|%, but not =.

Resources