How to add a goto to a for loop - batch-file

I am working on a script that edits files and I am building a choice goto menu.
The script itself works fine, it reads from a file name that has ( ) in it. So my files are saved like this (1) filename.txt, (2) filename.txt ....(100) filename.txt
the content of the file has "times": 125489, saved in it, the script removes the 100 from the file name and does this "times": 100,
This is a small part of my content for my txt files it has a 4 indent space per line I believe the format is Json
"aidr": 3.58,
"nlpr": 0.5,
"tafr": 0.5,
"titp": 0.5,
"trld": 0.0,
"tssp": 0.5,
"tssr": 0.5,
"ttup": 0.5,
"ttpp": 0.5,
"times": 125,
"Stamp": 125,
"ppiid": 649,
"otiid": 649,
"apcid": 9,
"orcid": 9,
"jpcns": 0,
"agpns": 0,
"opcns": 0,
"rppns": 0,
I recently found that when I merged all my single scripts together it slows down dramatically. Before the script processed 500 .txt files in 1 minute but now it takes 30 minutes.
My goal is to find a way to bring back the speed of this script.
I heard and read that adding a goto before and after the loop could make the script fast again.
#echo off
:Menu
ECHO ################################################################
echo.
ECHO 1 - Script 1
ECHO 2 - Script 2
ECHO 3 - Script 3
ECHO 4 - Script 4
ECHO 5 - Script 5
ECHO 100 - Script 100
echo.
set pass=
:: the choice command
set /p Mchoice=Make Your Choice:
::goto choices
goto=:%Mchoice%
goto %goto%
:1
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
set "ToReplace1="times": "
SET "sourcedir=New folder 1"
SET "destdir=New fodler 2"
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq "!line:times=!" CALL :Nums1 %%j
echo(!line!
)
)
)>"%destdir%\%%q"
)
GOTO :Menu
:Nums1
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace1%%%s=%ToReplace1%!"
IF "%original%" neq "%line%" goto Nums1
set "line=!line:%ToReplace1%=%ToReplace1%%1!"
GOTO :eof
:2
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
set "ToReplace2="Stamp": "
SET "sourcedir=New folder 1"
SET "destdir=New fodler 2"
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq "!line:Stamp=!" CALL :Nums2 %%j
echo(!line!
)
)
)>"%destdir%\%%q"
)
GOTO :Menu
:Nums2
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace2%%%s=%ToReplace2%!"
IF "%original%" neq "%line%" goto Nums1
set "line=!line:%ToReplace2%=%ToReplace2%%1!"
GOTO :eof
:15
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
cd /d "%~dp0"
set "ToReplace1="times": "
set "ToReplace2="Stamp": "
set "ToReplace3="jpcns": "
SET "sourcedir15=New folder 1"
SET "destdir15=New fodler 2"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%sourcedir15%\(*)*.txt"') DO (
SET "newdest=%%~dpq"
SET "newdest=!newdest:%sourcedir15%=%destdir15%!"
SET "newdest=!newdest:~0,-1!"
MD "!newdest!" 2>nul
(
FOR /f "tokens=1 delims=()" %%j IN ("%%~nxq") DO (
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq "!line:times=!" CALL :Nums15 %%j
if defined line IF "%%b" neq "!line:Stamp=!" CALL :Nums15 %%j
if defined line IF "%%b" neq "!line:jpcns=!" CALL :Nums15 %%j
echo(!line!
)
)
)>"!newdest!\%%~nxq"
)
GOTO :Menu
:Nums15
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace1%%%s=%ToReplace1%!"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace2%%%s=%ToReplace2%!"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace3%%%s=%ToReplace3%!"
IF "%original%" neq "%line%" goto Nums15
set "line=!line:%ToReplace1%=%ToReplace1%%1!"
set "line=!line:%ToReplace2%=%ToReplace2%%1!"
set "line=!line:%ToReplace3%=%ToReplace3%%1!"
GOTO :eof

I've done a few exercises on this problem, but I've been unable to locate anything that might result in a 30:1 speed difference.
This version
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:times=!" (ECHO %%c) ELSE (
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: %%j,
)
) ELSE ECHO(
)
)
)>"%destdir%\%%q"
)
Seemed to be a little faster.
This:
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
SET "skipover="
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%sourcedir%\%%q"') do (
IF DEFINED skipover (ECHO(%%b
) ELSE (
set "line=%%b"
if defined line IF "%%b" neq "!line:times=!" SET "skipover=y"&CALL :Nums1 %%j
echo(!line!
)
)
)
)>"%destdir%\%%q"
)
uses a Boolean variable skipover to bypass some of the processing once the target line has been found and manipulated - but it actually seemed slower.
This
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:times=!" (ECHO %%c) ELSE (
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: %%j,
)
) ELSE ECHO(
)
)
)>"%destdir%\%%q"
)
was consistently a little faster than the original - it avoids the subroutine call.
And this:
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
SET "skipover="
for /F "usebackqdelims=" %%e in ("%sourcedir%\%%q") do (
IF DEFINED skipover (ECHO/%%e
) ELSE (
set "line=%%e"
if defined line (
IF "%%e" equ "!line:times=!" (ECHO %%e
) ELSE (
SET "skipover=Y"
for /F "tokens=1* delims=:" %%v in ("%%e") do ECHO %%v: %%j,
)
) ELSE ECHO/
)
)
)
)>"%destdir%\%%q"
)
was consistently nearly twice as fast, using the skip-if-it's been-changed technique and avoiding findstr. It won't reproduce empty lines, though.
I also found that the position of the target in the source was signiificant. Using a 700-line version of the sample data, placing the target at the end was half-speed of placing it at the start of the file, so I was able to get nearly a 4:1 speed difference between the original and this last technique if the target was placed early in the source data (running 1,000 700-line files).
I presume that sub appeared in the original post by way of editing-out the irrelevant portions of the code, but it seemed to be reversed in sense, the way it was written...
--- based on version 3,
:: remove variables starting #
FOR /F "delims==" %%e In ('set # 2^>Nul') DO SET "%%e="
:: Read controls from file "%1"
FOR /f "usebackqtokens=1*" %%b IN ("%~1") DO (
SET "#%%b=%%c"
FINDSTR /x /i /L /c:":#%%b" <"%~f0" >NUL
IF ERRORLEVEL 1 ECHO LABEL ":#%%b" NOT found&SET "destdir="
)
IF NOT DEFINED destdir ECHO Required LABEL(s) NOT found &GOTO :eof
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
rem %%q contains the name of the file to be processed
rem remove variables starting $
FOR /F "delims==" %%e In ('set $ 2^>Nul') DO SET "%%e="
rem extract filenumber if sourcefilename is (number)something
SET "filenumber="
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO SET "filenumber=%%j"
rem
rem now process the file
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
rem if any #name is detected, take action
SET "processline=Y"
FOR /f "tokens=1,2delims=#=" %%u IN ('set #') DO IF DEFINED processline IF "%%c" neq "!line:%%u=!" (
SET "processline="
CALL :#%%u %%v
)
IF DEFINED processline ECHO %%c
) ELSE ECHO/
)
)>"%destdir%\%%q"
)
GOTO :eof
:#times
rem replace times value with filenumber
ECHO "times": %filenumber%,
GOTO :eof
:#titp
rem replace titp value with value read from file (%1)
ECHO "titp": %1,
GOTO :eof
:#tssp
rem put newz value before and zwen value after tssp value
ECHO "newz": whatever,
ECHO %line%
ECHO "zwen": revetahw,
GOTO :eof
:#trld
rem delete all trld values
GOTO :eof
:#tssr
rem replace the third and the fifth tssr value (I have multiples)
rem note that all $ variables are deleted FOR each file
SET /a $#tssr+=1
IF %$#tssr%==3 ECHO "tssr": thirdtime,&GOTO :eof
IF %$#tssr%==5 ECHO "tssr": fifthtime,&GOTO :eof
ECHO %line%
GOTO :eof
Using a control file contents
times #
titp jello
trld #
tssp #
tssr #
So - this is an attempt to obviate the repeated cloning of the file-processing logic.
First, I'm setting all variables that start # to nothing, un-defining them. That way, the remainder of the code knows that any variable starting # has not been inherited from elsewhere.
Next step : read the file to set up
#times=#
#titp=jello
#trld=#
#tssp=#
#tssr=#
The code then examines itself to make sure that subroutines :#times etc all exist and complains if they don't. destdir is simply a convenient non-empty variable used as a flag - we'll abort if the subroutines are missing, so no problem there.
Next - read the filenames. Obviously, the filemask may change depending on the selection made from the menu.
Then, for each file, kill all $ variables for the same reason as # but this time for each file to be processed.
Then set up variables to contain whatever data about the file is appropriate. Could be size, date, whatever - extract-parenethesised-number is shown. No harm in doing such things for all files, even if the data is not used for this particular menu selection.
Then process the file, detecting the keystrings from the # array. Call :#keystring if found, regurgitate line if not.
Each :# routine does what it does. A variety of possibilities is shown. Naturally, any particular routine could be controlled further by testing the selection made.
Setting the value assigned to #? to a value other than # will deliver that value to the :#? subroutine as parameters. Use them as you will - constants, whatever - just interpret the parameters delivered.
Naturally, you could set the # variables manually for particular menu selections instead of reading them from a file.
But - this is drifting way away from the timing problem - just adding more facilities (I'm a chronic implementor of bells and whistles) so I'd suggest to end discussion here.

Related

Add numbers with same text together

I would like to create a batch file in which I can see what I have collected in a game.
The game saves this information in a .txt file.
The output would look like this.
70x Silver.
Back Pearl.
41x Copper.
Amethyst.
Amethyst.
12x Silver.
Back Pearl.
21x Copper.
5x Silver.
Back Pearl.
Back Pearl.
Amethyst.
What I want to do now, is to add the items with the same name together, like this:
128x Silver.
4x Back Pearl.
62x Copper.
3x Amethyst.
There are hundreds of items with different names, not just these 4.
Would that be possible?
Any help would be appreciated. Thanks!
Another one!
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%l in (test.txt) do for /F "tokens=1*" %%a in ("%%l") do (
set "first=%%a"
if "!first:~-1!" equ "x" (set /A "num=!first:~0,-1!") else set "num=0"
if !num! equ 0 (
set "rest=%%l"
set /A "count[!rest: =_!]+=1"
) else (
set "rest=%%b"
set /A "count[!rest: =_!]+=num"
)
)
(for /F "tokens=2* delims=[]=" %%a in ('set count[') do (
set "item=%%a"
if %%b equ 1 (
echo !item:_= !
) else (
echo %%bx !item:_= !
)
)) > summary.txt
#ECHO OFF
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q72672485.txt"
:: remove variables starting #
FOR /F "delims==" %%b In ('set # 2^>Nul') DO SET "%%b="
FOR /f "usebackqdelims=" %%b IN ("%filename1%") DO (
CALL :sub %%b
)
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /F "tokens=1,2delims==" %%b In ('set # 2^>Nul') DO (
SET "line=%%cx%%b"
ECHO !line:#= !
)
)>summary.txt
endlocal
type summary.txt
GOTO :EOF
:sub
SET "quantity=%1"
SET "line=%*"
IF /i "%quantity:~-1%"=="x" (SET /a quantity=%quantity:~0,-1%&SET "line=%line:* =%") ELSE SET quantity=1
IF %quantity%==0 SET /a quantity=1&SET "line=%*"
SET /a #%line: =#%+=quantity
GOTO :eof
Different approach...
Would that be possible? - Yes.
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (t.txt) do (
set " z=%%a"
set " z=!z:x =#!"
set " z=!z: =_!"
for /f "tokens=1,2 delims=#" %%b in ("!z!") do (
if "%%c" == "" (
set "x=1"
set "y=%%b
) else (
set "x=%%b"
set "y=%%c"
)
set /a #!y!+=!x!
)
)
(for /f "tokens=1,2 delims=#=" %%a in ('set #') do (
set "x=%%a"
set "y=%%bx "
echo !y:~0,4! !x:_= !
))>summary.txt
Output with your example data (I hope, alphabetic sorting is ok for you):
3x Amethyst.
4x Back Pearl.
62x Copper.
87x Silver.
(your calculation of 120 silver might be a bit optimistic with the given input data)
This is a different approach that use a "file merge" method. The overall code is somewhat simpler than other methods...
#echo off
setlocal EnableDelayedExpansion
set "lineNum=0"
(for /F "tokens=1,2* delims=:x " %%a in ('(type test.txt ^& echo 0x^) ^| findstr /N "[0-9][0-9]*x"') do (
if !lineNum! lss %%a call :locateLine %%a
set "line=%%c"
set /A "count[!line: =_!]+=%%b"
)) < test.txt
set "count[="
(for /F "tokens=2* delims=[]=" %%a in ('set count[') do (
set "item=%%a"
if %%b equ 1 (echo !item:_= !) else echo %%bx !item:_= !
)) > summary.txt
goto :EOF
:locateLine num
set /A "lineNum+=1" & set /P "line=" & if errorlevel 1 exit /B
if %lineNum% lss %1 set /A "count[%line: =_%]+=1" & goto locateLine
exit /B
Another approach (splitting the file into items with and without quantity) (also fixing the Pollux Infusion issue in my first answer):
#echo off
setlocal enabledelayedexpansion
REM process lines without quantity:
for /f "delims=" %%a in ('type test.txt^|findstr /vrc:"[0123456789][0123456789]*x "') do call :count 1 "%%a"
REM process lines with quantity:
for /f "tokens=1*delims=x " %%a in ('type test.txt^|findstr /rc:"[0123456789][0123456789]*x "') do call :count %%a "%%b"
REM reformat:
(for /f "tokens=1,2 delims=#=" %%a in ('set #') do (
set "count=%%bx "
set "line=!count:~0,5!%%a" &REM including '1x'
if %%b==1 set "line= %%a" &REM supressing '1x'
echo !line:_= !
))>summary.txt
type summary.txt
goto :eof
:count
set item=%~2
set "item=%item: =_%"
set /a #%item% +=%1
Included both with and without 1x. Remove the line, you don't want.

Change file content using batch

So I have this going and I need a little help to improve it
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
cd /d "%~dp0"
:: Significant part of string
set "replacemain="appi": "
#For %%G In ("%~dp0Originals") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0Ready") Do Set "destdir=%%~fG"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%sourcedir%\(*)*.txt"') DO (
rem calculate new destination directory
SET "newdest=%%~dpq"
SET "newdest=!newdest:%sourcedir%=%destdir%!"
SET "newdest=!newdest:~0,-1!"
MD "!newdest!" 2>nul
(
FOR /f "tokens=1 delims=()" %%j IN ("%%~nxq") DO (
rem %%j now has sequence number
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq "!line:appi=!" CALL :subs %%j
echo(!line!
)
)
)>"!newdest!\%%~nxq"
)
GOTO :eof
:: substitute
:subs
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%replacemain%%%s=%replacemain%!"
IF "%original%" neq "%line%" goto subs
set "line=!line:%replacemain%=%replacemain%%1!"
GOTO :eof
This is designed to read anything in the filename in closed by ( )
Main Folder
|
+-\Orignal
|--| + - (100) Craft.txt
|--------------"appi": 4748483,
+-\Ready
|--| + - (100) Craft.txt
|--------------"appi": 100,
My script is based on 1 code editing and now I have been trying to change it to multiple code editing
As you can see here, this is set for one code I would like to change it to a type of dictionary
set "replacemain="appi": "
My goal
replace=replacemain = ("appi":
"code":
"gedt":)
set "replace"
if defined line IF "%%b" neq "!line:replace=!" CALL :subs %%j
echo(!line!
Could this be possible, and all codes will change to match the file name in the (?)
Also is there a way to increase the speed of the script
Thank you
This is how it looks inside my txt file
BEFORE
(100) filename.txt
|----"appi": 4748483,
"code": 415241,
"gedt": 458421,
AFTER
(100) filename.txt
|----"appi": 100,
"code": 100,
"gedt": 100,

how to remove a Negative using a txt editing batch script

I need a little help with this
I recently asked a different question on this same script
Now I have my scripted working for me and found a new issue
Here is my script
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
cd /d "%~dp0"
:: Significant part of string
set "params1="okhg": "
set "params2="tfed": "
set "params3="pkna": "
set "params4=txt": ""
#For %%G In ("%~dp0Preparing") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0Ready") Do Set "destdir=%%~fG"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%sourcedir%\(*)*.txt"') DO (
rem calculate new destination directory
SET "newdest=%%~dpq"
SET "newdest=!newdest:%sourcedir%=%destdir%!"
SET "newdest=!newdest:~0,-1!"
MD "!newdest!" 2>nul
(
FOR /f "tokens=1 delims=()" %%j IN ("%%~nxq") DO (
rem %%j now has sequence number
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%%q"') do (
set "line=%%b"
for %%x in (%params1%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs1 %%j
for %%x in (%params2%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs2 %%j
for %%x in (%params3%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs3 %%j
for %%x in (%params4%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs4 %%j
echo(!line!
)
)
)>"!newdest!\%%~nxq"
)
GOTO :eof
:: substitute
:subs1
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params1%%%s=%params1%!"
IF "%original%" neq "%line%" goto subs1
set "line=!line:%params1%=%params1%%1!"
GOTO :eof
:subs2
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params2%%%s=%params2%!"
IF "%original%" neq "%line%" goto subs2
set "line=!line:%params2%=%params2%%1!"
GOTO :eof
:subs3
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params3%%%s=%params3%!"
IF "%original%" neq "%line%" goto subs3
set "line=!line:%params3%=%params3%%1!"
GOTO :eof
:subs4
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params4%%%s=%params4%!"
IF "%original%" neq "%line%" goto subs3
set "line=!line:%params4%=%params4%CITY%1!"
GOTO :eof
This is how it works
ORIGNAL FOLDER
+ ----- FOLDER 1
--------+ --(100) Filename.txt
-------------+ -- "okhg": 452587"
-------------+ -- "tfed": 6541"
-------------+ -- "pkna": -58452"
-------------+ -- "txt": ""
RESULTS FOLDER
+ ----- FOLDER 1
--------+ --(100) Filename.txt
-------------+ -- "okhg": 100"
-------------+ -- "tfed": 100"
-------------+ -- "pkna": 100-58452"
-------------+ -- "txt": "CITY100"
The issue I am coming across with is that this line has a negative
"pkna": -58452"
I have tested different ways to make the scripts remove the negative
if I leave it like this, I get this result
set "params3="pkna": "
"pkna": 100-58452"
if I do this, I get this result
set "params3="pkna": -"
"pkna": -100"
what I am looking for is this
"pkna": 100"
Thank you
Edit:
The question has been updated with an answer.
The code will "code" multiple row/lines in to a text file using its prefix name, or with a "more direct" coding.
If you do know a way to clean up the code feel free to post an answer to do so., I would like to leave a script that I can continue to add more codes if needed
Thank you for all the hard work you'll provide us with to make our scripts better
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
cd /d "%~dp0"
:: Significant part of string
set "params1="okhg": "
set "params2="tfed": "
set "params3="pkna": "
set "params4=txt": ""
#For %%G In ("%~dp0Preparing") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0Ready") Do Set "destdir=%%~fG"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%sourcedir%\(*)*.txt"') DO (
rem calculate new destination directory
SET "newdest=%%~dpq"
SET "newdest=!newdest:%sourcedir%=%destdir%!"
SET "newdest=!newdest:~0,-1!"
MD "!newdest!" 2>nul
(
rem this will take the data in the filename enclosed in (!)
FOR /f "tokens=1 delims=()" %%j IN ("%%~nxq") DO (
rem this will replace any random codes found after : with 10
FOR /f "tokens=1 delims=()" %%k IN ("10") DO (
rem this will add any wording after this : and will take data enclosed in (!)
FOR /f "tokens=1 delims=()" %%l IN ("%%~nxq") DO (
rem %%j %%k %%l now has sequence number or pattern
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%%q"') do (
set "line=%%b"
rem in order to make each code work I gave them there own ::Substitute
for %%x in (%params1%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs1 %%j
for %%x in (%params2%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs2 %%j
for %%x in (%params3%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs3 %%k
for %%x in (%params4%) do if defined line IF "%%b" neq "!line:%%x=!" CALL :subs4 %%l
echo(!line!
)
)
)
)
)>"!newdest!\%%~nxq"
)
GOTO :eof
:: substitute
:subs1
SET "line=%line:-=%"
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params1%%%s=%params1%!"
IF "%original%" neq "%line%" goto subs1
set "line=!line:%params1%=%params1%%1!"
GOTO :eof
:subs2
SET "line=%line:-=%"
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params2%%%s=%params2%!"
IF "%original%" neq "%line%" goto subs2
set "line=!line:%params2%=%params2%%1!"
GOTO :eof
:subs3
SET "line=%line:-=%"
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params3%%%s=%params3%!"
IF "%original%" neq "%line%" goto subs3
set "line=!line:%params3%=%params3%%1!"
GOTO :eof
:subs4
SET "line=%line:-=%"
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%params4%%%s=%params4%!"
IF "%original%" neq "%line%" goto subs3
set "line=!line:%params4%=%params4%City%1!"
GOTO :eof

Windows Batch File: First word of File Name as column 1

The below code from computing.net is almost completely useful but needs minor changes. I have no clue as to how this is working. Can someone please help ?
for %%a in (*.csv) do call :addId "%%~Na" "%%a"
goto :EOF
:addId
#echo off
for /f "usebackq delims=" %%b in (%2) do (
> #.csv echo ID,%%b
goto :next
)
:next
for /f "usebackq skip=1 delims=" %%b in (%2) do (
>> #.csv echo %~1,%%b
)
move #.csv %2
Need to remove the code that inserts a header "ID"
Use ONLY the first word of file NOT complete file name --- as column 1 value
Here, word means collection of alphabets that are separated by space. So 1st word will be collection of alphabets before 1st space.
Could someone please help ?
Use for /f parsing loops:
#echo off
for %%a in (*.csv) do (
echo Processing %%a
for /f "tokens=1 eol=*" %%b in ("%%~na") do (
set /p header=<"%%a"
call echo %%header%%
for /f "skip=1 usebackq delims=" %%c in ("%%a") do echo %%b,%%c
) >"%%a.new"
move /y "%%a.new" "%%a" >nul
)
pause
This will preserve the header line intact.
Can you try this? PLease let me know how it works.
setlocal enabledelayedexpansion
for %%a in (*.csv) do call :addId "%%~Na" "%%a"
goto :EOF
:addId
#echo off
for /f "usebackq delims=" %%b in (%2) do (
rem > #.csv echo ID,%%b
goto :next
)
:next
for /f "usebackq skip=1 delims=" %%b in (%2) do (
for /f "tokens=1 delims= " %%x in ("%~1") do set firstWord=%%x
>> #.csv echo !firstWord!,%%b
)
move #.csv %2

Dos batch script error? Error occurred while processing: .exe

In a DOS script that I wrote, I am unable to figure out what causes this error that I get:
The system cannot find the file specified.
Error occurred while processing: .exe.
Here is the script. Any help would be greatly appreciated. I tried to ask for help on the DosTips forum but I am getting no answer. :
#echo off
:: script to edit property files
CALL :PROPEDIT # Key4 Value446 test.properties
GOTO :END
:PROPEDIT [#] PropKey PropVal File
IF "%~1"=="#" (
:: Passing a first argument of "#" will disable the line while editing
SET "_PREFIX=#"
SHIFT
)
IF NOT "%~4"=="" (
ECHO Too many arguments.
EXIT /B 1
)
IF "%~3"=="" (
ECHO PROPEDIT: Function requires 3 args: [#] PropKey PropVal File
EXIT /B 1
) ELSE (
SET "_PROPKEY=%~1"
SET "_PROPVAL=%~2"
SET "_FILE=%~3"
)
MOVE /Y "%_FILE%" "%_FILE%.bak">nul
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%_FILE%.bak" ^|FINDSTR /N /I "%_PROPKEY%="`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=:" %%S IN ("%LINE%") DO SET LINE=%%S
SET /A COUNT=1
FOR /F "USEBACKQ delims=" %%A IN (`TYPE "%_FILE%.bak" ^|FIND /V /N ""`) DO (
SET "LN=%%A"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "LN=!LN:*]=!"
IF "!COUNT!" NEQ "%LINE%" (
ECHO(!LN!>>%_FILE%
) ELSE (
ECHO %_PREFIX%%_PROPKEY%=%_PROPVAL%>>%_FILE%
ECHO Updated '%_FILE%' with value '%_PREFIX%%_PROPKEY%=%_PROPVAL%'.
)
ENDLOCAL
SET /A COUNT+=1
)
EXIT /B 0
:END
ECHO --- Finished Test ---
pause
Remove the .EXE of FIND and TYPE
You don't need TYPE. You can do just this:
FOR /F "tokens=*" %%A IN (`FIND /N /I "%_PROPKEY%=" "%_FILE%.bak"`) DO (
If FIND spoils your results (by not using TYPE) then consider using FINDSTR instead and use 'DELIMS=:' instead of 'DELIMS=]'
If I'm right my assumption that the following is helpful, take a look at the 'MORE +nnn' command (note the '+nnn' which outputs lines from a specific location in the file).
Why not just place your 'SETLOCAL ENABLE.. etc' at the top of your code?
If you explain what it is you're trying to attempt, then I might be in a better position to help.
Just a few thoughts :)
Here is the working code after getting some help from Paul Tomasi:
#echo off
SETLOCAL DISABLEDELAYEDEXPANSION
CALL :PROPEDIT # Key4 Value446 test.properties
GOTO :END
:PROPEDIT [#] PropKey PropVal File
IF "%~1"=="#" (
:: Passing a first argument of "#" will disable the line while editing
SET "_PREFIX=#"
SHIFT
)
IF NOT "%~4"=="" (
ECHO Too many arguments.
EXIT /B 1
)
IF "%~3"=="" (
ECHO PROPEDIT: Function requires 3 args: [#] PropKey PropVal File
EXIT /B 1
) ELSE (
SET "_PROPKEY=%~1"
SET "_PROPVAL=%~2"
SET "_FILE=%~3"
)
MOVE /Y "%_FILE%" "%_FILE%.bak">nul
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE "%_FILE%.bak" ^|FINDSTR /N /I "%_PROPKEY%="`) DO (
SET LINE=%%A
)
FOR /F "tokens=1,2* delims=:" %%S IN ("%LINE%") DO SET LINE=%%S
SET /A COUNT=1
FOR /F "USEBACKQ delims=" %%A IN (`TYPE "%_FILE%.bak" ^|FIND /V /N ""`) DO (
SET "LN=%%A"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "LN=!LN:*]=!"
IF "!COUNT!" NEQ "%LINE%" (
ECHO(!LN!>>%_FILE%
) ELSE (
ECHO %_PREFIX%%_PROPKEY%=%_PROPVAL%>>%_FILE%
ECHO Updated '%_FILE%' with value '%_PREFIX%%_PROPKEY%=%_PROPVAL%'.
)
SETLOCAL DISABLEDELAYEDEXPANSION
SET /A COUNT+=1
)
EXIT /B 0
:END
ECHO --- Finished Test ---
pause

Resources