I need to get a batch file running, which helps our employees to change text files without trouble.
As you can see, there are a lot of whit space in this file and they need to be there, otherwise it could not be imported in other programs.
My question: Is it possible to search for a specific column and replace this value with an user input?
I've tried to get something done with help of google and this was the result:
#echo off
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~126,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
echo R_sph neu: %R_sph_new%
set "search=R_sph"
set "replace=R_sph_new"
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
del %textfile%
rename %newfile% %textfile%
pause
PS: My batch experience is worse and it would be great to explain every step in the code you send me.
Edit: We generate a lot of files like this every day and only some of them need so be edited. The construction of this files is always the same but with other values so it's important so search via column.
Change the main loop in your batch file so that:
it only changes the 6th line;
it only changes the 5 characters from position 126 to 130.
And just to make the batch script more robust, you can also validate user input to ensure that they've entered exactly 5 characters as the replacement string.
Here's the whole file with suggested changes:
#echo off
setlocal enabledelayedexpansion
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~126,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
echo R_sph neu: %R_sph_new%
rem Validate user input - ensure it's exactly 5 characters
set "replace=%R_sph_new% "
set "replace=%replace:~0,5%"
if not "%R_sph_new%" == "%replace%" (
echo Don't be silly.
exit /b 1
)
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
set /a lineNumber = 0
(for /f "delims=" %%i in (%textfile%) do (
set /a lineNumber += 1
set "line=%%i"
if !lineNumber! == 6 set "line=!line:~0,126!%replace%!line:~131!"
echo(!line!
))>"%newfile%"
rem It might be safer to rename %textfile% to %textfile%.BAK instead of deleting it.
del %textfile%
rename %newfile% %textfile%
endlocal
pause
set "search=R_sph"
set "replace=R_sph_new"
should
set "search=%R_sph%"
set "replace=%R_sph_new%"
that is, set the values of search and replace to the contents of the variables. Your code sets search to the string R_sph.
? Are you sure that the value in R_sph will be unique in your file?
Thank you very much Klitos Kyriacou, that's exactly what I was looking for!
But unfortunately this batch does not change anything in this text file, the whole code looks like this:
#echo off
:R_sph
for /f "skip=5 delims=" %%a in (Muster.dat) do set "var=%%a"&goto :done
:done
set "R_sph=%var:~49,5%"
echo R_sph: %R_sph%
set /p R_sph_new=Enter new sph:
set "replace=%R_sph_new%"
set "textfile=Muster.dat"
set "newfile=Muster2.dat"
setlocal enabledelayedexpansion
set /a lineNumber = 0
(for /f "delims=" %%i in (%textfile%) do (
set /a lineNumber += 1
set "line=%%i"
if !lineNumber! == 6 set "line=!line:~0,49!%replace%!line:~54!"
echo(!line!
))>"%newfile%"
endlocal
:replace file
del %textfile%
rename %newfile% %textfile%
pause
Edit: I was in hurry and dind't get how to set it up. Now I changed the code and it's working!
Related
I have an XML file (generated by a third party tool) which is of format
<?xml version="1.0" encoding="UTF-8"?><ROOT test_count="22" test_fail_count="1" test_pass_count="21".......</ROOT>
All the content is in one line of the file with name Report.xml.
I have been trying to write a batch script
#echo off
setlocal EnableDelayedExpansion
set "Report="
for /f "delims=" %%x in (Report.xml) do set "Report=!Report! %%x"
REm set /p Report=< %Results_File%
echo report is !Report!
call:parseVal !Report!
exit/b
:parseVal
setlocal enableDelayedExpansion
set val="%~1"
echo %val%
echo !val!|findstr /rc:"test_count=.[0-9]*." >nul || (
echo !val!
EndLocal
exit /b
)
rem ..
Basically I am trying to grab values for test_count(22), test_fail_count(1) and test_pass_count(21) from the XML file.
Any ideas on how to achieve this?
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q42057260.txt"
SET "testcount="
SET "testfailcount="
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
SET "xmlline=%%a"
CALL :process
)
ECHO test count=%testcount% test fail count=%testfailcount%
GOTO :EOF
:process
:: dispose of awkward characters
SET "xmlline=%xmlline:?= %"
SET "xmlline=%xmlline:>= %"
SET "xmlline=%xmlline:<= %"
CALL :select %xmlline%
GOTO :EOF
:select
IF /i "%~1"=="" GOTO :EOF
IF DEFINED testcount IF DEFINED testfailcount GOTO :EOF
IF /i "%~1"=="test_count" SET /a testcount=%~2
IF /i "%~1"=="test_fail_count" SET /a testfailcount=%~2
SHIFT
GOTO select
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q42057260.txt containing your data for my testing.
Read the file to %%a and transfer to xmlline.
Use :process to remove awkward characters (those with a special meaning to cmd) by replacing each with a space, then pass the resultant line to :select as a parameter-list.
look at each parameter in turn, stripping quotes. When the target strings appear, set their storage variables to the following value. When both values are assigned, bail out.
and report results.
Provided the structure and order of Report.xml is constant this could do (cmd line)
for /f "tokens=5,6 delims=<> " %A in (Report.xml) Do #Set %A&Set %B
Sample output with your Report.xml
> set test
test_count="22"
test_fail_count="1"
So in Windows Explorer, I have these files sorted like this:
I have this script to remove the brackets and one zero, and in case the trailing number is greater than or equal to 10, to remove two zeroes:
cd C:\folder
setlocal enabledelayedexpansion
SET /A COUNT=0
for %%a in (*.jpg) do (
SET /A COUNT+=1
ECHO !COUNT!
set f=%%a
IF !COUNT! GTR 9 (
set f=!f:^00 (=!
) ELSE (
set f=!f:^0 (=!
)
set f=!f:^)=!
ren "%%a" "!f!"
)
pause
However, once I run the code, I get this result:
So the batch file isn't going through the files "intuitively" like Windows Explorer shows them. Is there any way to change this? Or is there a better way to rename these files altogether?
This uses a different approach:
#echo off
cd C:\folder
setlocal enabledelayedexpansion
SET /A COUNT=0, REMOVE=2
for /F "delims=(" %%a in ('dir /B *.jpg') do (
SET /A COUNT+=1
ECHO !COUNT!
set "f=%%a"
IF !COUNT! EQU 10 SET "REMOVE=3"
for /F %%r in ("!REMOVE!") do set "f=!f:~0,-%%r!"
ren "%%a" "!f!!COUNT!.jpg"
)
pause
Here is a method that does not depend on the sort order used by the file system, preserving the numbers as occurring in the original file names.
For each file name (for instance, test_this_01 SELR_Opening_00000 (1).jpg), the portion after the last under-score _ is retrieved (00000 (1)). Then the parentheses and the space are removed and then the length is trimmed to five characters (00001). This string replaces the original one in the file name finally (test_this_01 SELR_Opening_00001.jpg); the file name must not contain the replaced portion (00000 (1)) multiple times (hence file names like this should not occur: test_this_00000 (1) SELR_Opening_00000 (1).jpg):
#echo off
setlocal DisableDelayedExpansion
rem // Define constants here:
set "LOCATION=."
set "PATTERN=*_* (*).jpg"
set /A "DIGITS=5"
pushd "%LOCATION%" || exit /B 1
for /F "usebackq eol=| delims=" %%F in (`
dir /B /A:-D /O:D /T:C "%PATTERN%"
`) do (
set "FILE=%%F"
setlocal EnableDelayedExpansion
set "LAST="
for %%I in ("!FILE:_=","!") do (
set "LAST=%%~nI" & set "FEXT=%%~xI"
set "FNEW=!FILE:%%~I=!"
)
set "LAST=!LAST:(=!" & set "LAST=!LAST:)=!"
set "LAST=!LAST: =!" & set "LAST=!LAST:~-5!"
ECHO ren "!FILE!" "!FNEW!!LAST!!FEXT!"
endlocal
)
popd
endlocal
exit /B
Adapt the directory location and the file search pattern in the top section of the script as you like.
After having tested, remove the upper-case ECHO command in order to actually rename files.
I'm really close, I think. I'm not great at this kind of stuff, but I've cobbled together code I found searching here. Maybe you can figure out what I'm doing wrong. Here's what I've got:
#echo off
setlocal enableextensions disabledelayedexpansion
set /p "number=Please enter the starting control number:" || goto :eof
for /f "delims=" %%a in (List.txt) do (
setlocal enabledelayedexpansion
for %%b in (!number!) do (
endlocal
echo copy "%%~fa" "c:\myfolder\%%b_%%~nx"
)
set /a "number+=1"
)
So far it outputs as needed, except the file isn't created with theoriginal name at the end. It actually suffixes "%~nx" as text.
Essentially, I want it to take the original file, tack on a prefix, counting up from a provided number, padded (this part I haven't worked on yet), into a new folder.
Thanks for reading!
Here's what I neded up with:
setlocal enableextensions enabledelayedexpansion
set /p "dir=Set output dir:" || goto :eof
set /p "d=Set total digits:" || goto :eof
set number=1
for /f "delims=" %%a in (List.txt) do (
setlocal enabledelayedexpansion
for %%b in (!number!) do (
set "pad=0000000000%%b"
set "name=!pad:~-%d%!
copy "%%~fa" "%dir%\!name!_%%~nxa"
)
set /a "number+=1"
)
~nx is the name of a variable called x. Perhaps you meant either ~nxa or ~nxb which is name and extension of your two for variables.
Why is there an EndLocal in the middle of a loop?
I'm trying to read a file and output the lines of data into registry keys. The data collection works, but I don't understand the syntax required to increment the string values in the last loop.
#echo OFF
SETLOCAL DisableDelayedExpansion
FOR /F "usebackq skip=1 delims=" %%a in (`"findstr /n ^^ C:\GetSID.txt"`) do (
set "var=%%a"
SETLOCAL EnableDelayedExpansion
set "var=!var:*:=!" This removes the prefix
echo(!var:~76,63!>>C:\SIDoutput.txt
goto :EndLoop
)
:EndLoop
set /p SID= <C:\users\paintic\SIDoutput.txt
set KEY_NAME="HKEY_USERS\!SID!\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
set Counter=1
for /f %%x in (C:\users\paintic\Networkprinters.txt) do (
set "Line_!Counter!=%%x"
set /a Counter+=1
if !Counter!==3 (Echo %line_counter%)
)
set /a counter2=!counter!-3
set counter=1
The part below is what I can't get to work. I'm trying to write LINE_1, LINE_2 and LINE_3 values from the previous loop to increment via the loop below. So VALUENAME should equal LINE_1, TYPE should = LINE_2's value and DATA should = LINE_3 on the first run and keep going up by 1 until the loop finishes (end of the file read)
`for /L %%i in (1,1,%counter2%) do (
set ValueName=%Line_!counter!%
set /a counter+=1
set Type=%Line_!counter!%
set /a Counter+=1
set Data=%Line_!counter!%
set /a Counter+=1
echo !ValueName!
echo !Type!
echo !Data!
REG ADD %KEY_NAME% /v !ValueName! /t !Type! /d !Data! /f
)
ENDLOCAL
Pause`
On searching for errors in batch file it is always helpful to use in first line #echo on or remove #echo off or comment this line with rem to see what cmd.exe really executes.
Command line interpreter fails on lines with set VariableName=%Line_!counter!% as the interpreter does not know what to expand first. I think it is not possible to create dynamically the name of an environment variable and reference next the value of this environment variable. This approach most likely does not work ever.
However, what you want to achieve can be done much easier directly in second loop as the following example demonstrates:
#echo off
setlocal EnableDelayedExpansion
rem Create data for demo example.
set "KEY_NAME=HKEY_USERS\S-1-5-20\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
echo TestValue>"%TEMP%\Networkprinters.txt"
echo REG_SZ>>"%TEMP%\Networkprinters.txt"
echo Sample Data>>"%TEMP%\Networkprinters.txt"
echo AnotherValue>>"%TEMP%\Networkprinters.txt"
echo REG_DWORD>>"%TEMP%\Networkprinters.txt"
echo ^1>>"%TEMP%\Networkprinters.txt"
rem Now the loop follows which reads the data from the file line
rem by line and build the line for using command "reg.exe" to
rem add the data to registry of the user with the defined SID.
set Counter=1
for /f "usebackq delims=" %%x in ("%TEMP%\Networkprinters.txt") do (
if "!Counter!"=="1" (
set "ValueName=%%x"
) else if "!Counter!"=="2" (
set "ValueType=%%x"
) else (
set "ValueData=%%x"
rem Echo the command instead of really executing "reg.exe".
echo reg.exe ADD %KEY_NAME% /v "!ValueName!" /t !ValueType! /d "!ValueData!" /f
set Counter=0
)
set /a Counter+=1
)
rem Delete the text file created for demo example.
del "%TEMP%\Networkprinters.txt"
endlocal
This solution is much easier than what you have tried and can be maybe even more simplified.
I have a million old text files that I need to convert the format on. I have been desperately trying to do this myself but I really could use help. I am trying to convert data that looks like this:
text
11111.111
22222.222
33333.333
text2
44444.444
55555.555
66666.666
77777.777
88888.888
99999.999
(each number is on a seperate line and there are some blank lines, but I need them to go into the output file as a place keeper)
Into a .txt file that looks like this:
**I also need to add an increment number at the beginning of each line to number the lines.
1,11111.111,22222.222,33333.333,text
2,44444.444,55555.555,66666.666,text2
3,77777.777,88888.888,99999.999,
the files that I have are in separate folders in a directory and have no file extension but they behave exactly like a standard text file.
I have tried all sorts of stuff but I am just not that well versed in programming. Here is the little bit of code that I havent deleted for the 100th time. gettting frustrated
:REFORMAT
FOR %%F IN (*) DO IF NOT %%~XF==.BAT (
SETLOCAL DISABLEDELAYEDEXPANSION
(
SET /P LINE1=
SET /P LINE2=
SET /P LINE3=
SET /P LINE4=
)<"%%F"
ECHO %LINE2%,%LINE3%,%LINE4%,%LINE1%>>"%%F".TXT
PAUSE >NUL
:END
I am using windows I have access to dos6 dos7 winxp 32 and win7 64
the text and text2 are text strings within the file, they are descriptors telling me what the numbers below mean and some of the descriptors are left out. I also need it to process more than the first four lines. Some files have up to 200 lines inside of them. Thank you so much for helping me. thank you so much dbenham here is the final result:
#echo off
setlocal enableDelayedExpansion
for /R %%F in (*.) do (
set /a ln=0
for /f %%N in ('type "%%F"^|find /c /v ""') do set /a cnt=%%N/4
for /l %%N in (1 1 !cnt!) do (
for %%A in (1 2 3 4) do (
set "ln%%A="
set /p "ln%%A="
)
set /a ln+=1
echo !ln!,!ln2!,!ln3!,!ln4!,!ln1!
)
) <"%%F" >"%%F.CSV"
Using nothing but pure native batch:
#echo off
setlocal enableDelayedExpansion
for %%F in (*.) do (
set /a ln=0
for /f %%N in ('type "%%F"^|find /c /v ""') do set /a cnt=%%N/4
for /l %%N in (1 1 !cnt!) do (
for %%A in (1 2 3 4) do (
set "ln%%A="
set /p "ln%%A="
)
set /a ln+=1
echo !ln!,!ln2!,!ln3!,!ln4!,!ln1!
)
) <"%%F" >"%%F.txt"
The above will process all files that have no extension in the current folder. If you want to recursively include all sub-folders, then add the /R option to the outer FOR statement.
The whole thing can be done quite simply using my REPL.BAT utility:
#echo off
for %%F in (*.) do (
<"%%F" repl "([^\r\n]*)\r?\n([^\r\n]*)\r?\n([^\r\n]*)\r?\n([^\r\n]*)\r?\n?" "$2,$3,$4,$1\r\n" mx|findstr /n "^"|repl "^(.*):" "$1," >"%%F.txt"
)
just a few modifications:
set /p doesnt delete a variable if input is empty, so you have to delete it before.
You missed a closing paranthese )
You have to use delayed expansion to use a changed variable inside parantheses
the counter.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set count=0
FOR %%F IN (*.) DO IF NOT %%~XF==.BAT (
set "LINE1="
set "LINE2="
set "LINE3="
set "LINE4="
set count+=1
(
SET /P LINE1=
SET /P LINE2=
SET /P LINE3=
SET /P LINE4=
)<"%%F"
ECHO !count!,!LINE2!,!LINE3!,!LINE4!,!LINE1!>>"%%F.txt"
)
PAUSE >NUL
:END