Find and replace a string into a path in batch - search

I am trying a batch file which replaces a changing text named X by a static text 'English' into the path
Y:\SUT\ISTQB\Board\X\Dashboard\Open
About the position of the X text, it is always surrounded with 'Board' and 'Dashboard'.
In the path below, the current below text for the given path is 'X=Language' and my goal is finally to get 'X=English' each time of course!
Y:\SUT\ISTQB\Board\Language\Dashboard\Open
The string of replacement is read from a file. This file only contains 'English'
Is that possible to find a solution through a pipe with the 'find' command?
Thank you in advance

#ECHO OFF
SETLOCAL
SET mypath=Y:\SUT\ISTQB\Board\X\Dashboard\Open
FOR /f %%i IN (languagefile.txt) DO SET language=%%i
SET mypath=%mypath:\Dashboard\=*%
SET mypath=%mypath:\Board\=*%
FOR /f "tokens=1,3delims=*" %%i IN ("%mypath%") DO SET mypath=%%i\Board\%language%\Dashboard\%%j
ECHO %mypath%
"*" chosen because it cannot occur in a pathname.

Try this:
#echo off &setlocal enabledelayedexpansion
set "string=Y:\SUT\ISTQB\Board\Language\Dashboard\Open"
set "search=%string:*\Board\=%"
for /f "delims=\" %%i in ("%search%") do set "search=%%i"
set "string=!string:%search%=English!"
echo %string%

Related

Batch Trim Lines in Text File after Sub-String - REM Trim with Expansion

I am running Windows 10 Pro using batch files (open to using VBS and PS1 files) and I have a text file automatically exported by software that can look like this:
Sub_Group691_FE7IP11_2017-12-12.sldasm_bin/parts/Loft-Project.sldasm_bin/parts/Loft-Project...
Sub_Group691_FE7IP12_2017-12-12.sldasm_bin/parts/Loft-Project.sldasm_bin/parts/Loft-Project...
Sub_Group691_FE7IP13_2017-12-12.sldasm_bin/parts/Loft-Project.sldasm_bin/parts/Loft-Project...
Each line continues specifying sub-parts after the "..." and could contain "poison" characters. The Sub_Group part is pulled from the filename and can also contain "poison" characters.
What I am looking to do is export just the filename which is right at the beginning of each line, up to and including the first file extension, in this case ".sldasm." Everything to the right of the first instance of .sldasm should be trimmed.
What I have cobbled together so far from research on Stackoverflow is:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "inputfile=C:\Scratch\ASMExport.txt"
SET "outputfile=C:\Scratch\InputFiles.txt"
(
FOR /f "usebackqdelims=" %%a IN ("%inputfile%") DO (
SET "currentline=%%a"
ECHO("!currentline:.sldasm=.sldasm & rem "!"
)
)>"%outputfile%"
GOTO :EOF
My problem lies with the "rem" line, which does not seem to work as intended either because of being within a FOR loop or because of needing to enable delayed expansion. It seems to be parsing the "& rem" as text, which looks to be because of the way delayed expansion works. What I get from the above lines is:
SubGroup691_FE7IP11_2017-12-12.sldasm" & rem "/bin/parts/Loft-Project.sldasm" & rem ""
SubGroup691_FE7IP12_2017-12-12.sldasm" & rem "/bin/parts/Loft-Project.sldasm" & rem ""
SubGroup691_FE7IP13_2017-12-12.sldasm" & rem "/bin/parts/Loft-Project.sldasm" & rem ""
I can use this same line outside the loop and without ENABLEDELAYEDEXPANSION like this:
#ECHO OFF
SETLOCAL
SET "inputstring=Sub_Group691_FE7IP11_2017-12-12.sldasm_bin/parts/Loft-Project.sldasm_bin/parts/Loft-Project"
SET "outputstring=%inputstring:.sldasm=.sldasm & rem "%"
echo %outputstring%
The output to that would give me what I am looking for:
Sub_Group691_FE7IP11_2017-12-12.sldasm
In searching, I am beginning to think that rem cannot be used in this way, and I must move to a token delimiter loop using a bogus delimiter.
I would be content in getting this to work and not worrying about "poison" characters in the filename by being diligent about naming files correctly.
Thank you for your help!
The & rem approach to truncate strings to the right cannot work with delayed expansion (!), it relies on normal/immediate expansion (%). This is because immediate expansion is done before commands (like rem) are recognised, but delayed expansion happens afterwards, so the rem command is never executed.
However, you could in the loop replace every .sldasm by a forbidden character like | and then split the string by a for /F loop, like this:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "InputFile=C:\Scratch\ASMExport.txt"
set "OutputFile=C:\Scratch\InputFiles.txt"
set "Extension=.sdlasm"
> "%OutputFile%" (
for /F usebackq^ delims^=^ eol^= %%A in ("%InputFile%") do (
set "CurrentLine=%%A"
setlocal EnableDelayedExpansion
set "CurrentLine=!CurrentLine:%Extension%=|!"
for /F "delims=|" %%B in ("!CurrentLine!") do (
endlocal
echo(%%B
setlocal EnableDelayedExpansion
)
endlocal
)
)
endlocal
exit /B
Delayed expansion is toggled so that no for variables become expanded when it is enabled, in order to avoid loss of or problems with exclamation marks.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q47788829.txt"
FOR /f "usebackqtokens=1delims=/" %%a IN ("%filename1%") DO (
FOR /f "tokens=1*delims=." %%b IN ("%%a") DO (
FOR /f "tokens=1*delims=_" %%d IN ("%%c") DO (
ECHO %%%%-a=%%~a %%%%-b=%%~b %%%%-c=%%~c %%%%-d=%%~d %%%%-e=%%~e ^<^<
)
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q47788829.txt containing your data for my testing.
You should be able to assemble your required report data from the elements %%a..%%e displayed. All a matter of using tokens and delims constructively.
If your input is the text that you wrote, with the same structure, I think this batch should work and extract the names that you want quickly.
CODE:
#echo off
setlocal enabledelayedexpansion
set "inputFile=input.txt"
set "outputFile=result.txt"
for /f "delims=/ tokens=1" %%A in (%inputFile%) do (
set "tempFileName=%%A"
echo !tempFileName:~0,-4!>> %outputFile%
)
You only had to adjust the variables 'inputFile' and 'outputFile' to your needs.

Concatenate string and number in batch file

How to concatenate number and string in for loop?
I've tried like this but it doesn't work:
SET PATH=C:\file
FOR /L %%x IN (1,1,5) DO (
SET "NUM=0%%x"
SET VAR=%PATH%%NUM%
ECHO %VAR%
)
Modify your code like this:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET PATH=C:\file
FOR /L %%x IN (1,1,5) DO (
SET "NUM=0%%x"
SET VAR=%PATH%!NUM!
ECHO !VAR!
)
You always have to use SETLOCAL ENABLEDELAYEDEXPANSION and !...! instead of %...% when working with variables which are modified inside a loop.
You'll need delayed expansion to accomplish that:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET FILEPATH=C:\file
FOR /L %%x IN (1,1,5) DO (
SET "VAR=%FILEPATH%0%%x"
ECHO !VAR!
)
ENDLOCAL & SET VAR=%VAR%
You do not need the interim variable NUM, you can concatenate the string portions immediately.
You should never change variable PATH as this is used by the system. Hence I changed it to FILEPATH.
Since SETLOCAL creates a new localised environment block, all variables defined or changed in there cannot be seen outside of that block. However, the last line in the above code demonstrates how the last concatenated value can be passed beyond ENDLOCAL.

batch file to remove a column in csv

I wrote this code so I can remove a column from a csv file.
#echo off
setlocal enableextensions enabledelayedexpansion
type nul > tmp.txt
SET /A COUNT=0
for /F "tokens=*" %%A in (d.csv) do (
set LINE="%%A"
set /A COUNT+=1
for /F "tokens=1,2,3,4,5,6,7,8,* delims=," %%a in (!LINE!) do (
set row[0]=%%a
set row[1]=%%b
set row[2]=%%c
set row[3]=%%d
set row[4]=%%e
set row[5]=%%f
set row[6]=%%g
set row[7]=%%h
)
echo !row[0]!,!row[2]!,!row[3]!,!row[4]!,!row[5]!,!row[6]! >>tmp.txt
echo.
)
endlocal
Test file:
A1,B1,C1,D1,la la,,1
A2,B2,C2,D2, ,fef 3,
A3,B3,C3,D3,be be ,bo,bo 1
A4,B4,C4,D4,tu tu,tu 7,881
Output file:
A1,C1,D1,la la,1,
A2,C2,D2, ,fef 3,
A3,C3,D3,be be ,bo,bo 1
A4,C4,D4,tu tu,tu 7,881
I don't get why in the output file at the first line the ,, is eliminated and a , added at the end. Also I am wondering if there is a better way to do this.
Thanks!
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "delims=" %%a IN (q29441490.txt) DO (
SET "line=%%a"
SET "line=!line:,= , !"
FOR /f "tokens=1,2*delims=," %%p IN ("!line!") DO (
SET "line=%%p,%%r"
SET "line=!line: , =,!"
ECHO(!line!
)
)
)>u:\newfile.txt
GOTO :EOF
I used a file named q29441490.txt containing your data for my testing.
Produces u:\newfile.txt
the separators between tokens are delimiter sequences, so ,, is seen as one separator, hence the fields appear moved by one place.
Grab each line, replace each , with , tokenise (you don't say explicitly, but you appear to want to eliminate the second column) so %%q gets the first column and %%r the remainder of the line following the second. Concatenate these, insert the comma and then reverse the substitution.
If you wanted to eliminate another column, then a different tokens element should be specified and the restructure of the line would need to be adjusted.

I need to insert a text above the line containing a particular string "[TOP]" in batch programming

E.g. the file abc.txt contains below text in it and i have to add text "infra" in it above [TOP]:
;plcd
;abcd
valueof=a,b,c
[TOP]
and the output should be like this:
;plcd
;abcd
valueof=a,b,c
infra
[TOP]
How to do this in batch programming.
#echo off
setlocal
set "file=abc.txt"
set "start="
for /f usebackq^ delims^=^ eol^= %%a in ("%file%") do (
if not defined start set "start=1" & break > "%file%"
if "%%~a"=="[TOP]" (>>"%file%" echo(infra)
>>"%file%" echo(%%a
)
For each line with data in the file, if it contains [TOP], echo infra. After it, echo the readed line.

Insert string at the beginning of everyline of a text document?

I am trying to insert a certain string with spaces "sBody = " before each line in a text document. I found this answer in other forums, but it does not work? A few questions I have about this script are, should "myfile" be the full filepath? Also, this script was originally made to insert a string at the end of the file, what do I need to take out to make it insert just one string at the beginning?
Thank you for all of your answers!
#echo off > newfile & setLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in (myfile) do (
echo STRING %%a STRING >> newfile
)
The script is creating a new file named 'newfile' reading in the original file 'myfile'. Once it's done you can delete 'myfile' and rename 'newfile' to 'myfile' if you like. This example replaces each <line> in the file with STRING <line> STRING.
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in (myfile) do (
echo STRING %%a STRING >> newfile
)
sed 's/.*/pattern &/' yourfile.txt -i
works for me, in a single command line

Resources