Using batch for processing Text file - batch-file

I am new to batch scripting and would like to process my input file with the content :
MD5 hash of sample.js,
81f87dd81ef88f59a57d95d9ede5f92e
MD5 hash of searchReplace.js,
3493b216e1024f0d6de417ef6c8b3962
MD5 hash of Select Anything.js,
009f2b911b50550502b87aeeeb969b55
The output should look like :
MD5 hash of sample.js,81f87dd81ef88f59a57d95d9ede5f92e
MD5 hash of searchReplace.js,3493b216e1024f0d6de417ef6c8b3962
MD5 hash of Select Anything.js,009f2b911b50550502b87aeeeb969b55
Can someone please help me out with it ?

try this:
#echo off
setlocal enableDelayedExpansion
set "info_file=.\example1.txt"
for /f "useback tokens=* delims=" %%a in ("%info_file%") do (
set "line=%%a"
if "!line:~0,3!" EQU "MD5" (
set "to_echo=!line!"
) else (
set "to_echo=!to_echo!,!line!"
echo !to_echo!
)
)
endlocal

This works:
#echo off
setlocal
call :procFile < input.txt > output.txt
goto :EOF
:procFile
set /P "line1="
if errorlevel 1 exit /B
set /P "line2="
echo %line1%%line2%
goto procFile
You may also read the file via a for /F command this way:
#echo off
setlocal EnableDelayedExpansion
set "line="
(for /F "delims=" %%a in (input.txt) do (
if not defined line (
set "line=%%a"
) else (
echo !line!%%a
set "line="
)
)) > output.txt

Related

Checking if an array element(file) has a specific extension

I'm trying to write a code that does the following. I have some files in a directory with specific extensions. I have made a vector that contains all of them. Now I will want to rename each file to something else depending on their extension. So for that I'm trying to parce the created vector with a for loop in which I check for each element extension.
For now I won't rename it just echo it on the screen if the file with the .elf extension is found. I wrote this code but I get no echo as in there would be no .elf file in my directory. Please help me correct this. Thanks.
#echo off
setlocal enabledelayedexpansion
cd C:\Users\uidr0938\Desktop\Copy
set path=C:\Users\uidr0938\Desktop\Copy
set /a index=0
for /r %%i in (*) do (
set value[!index!]=%%i
set /a index+=1
)
set /a limit=%index%-2
for /l %%a in (0;1;%limit%) do (
if !value[%%a]! equ *.elf (
echo !value[%%a]!
)
)
endlocal
try with :
....
for /l %%a in (0;1;%limit%) do (
if "!value[%%a]:~-4!" equ ".elf" (
echo !value[%%a]!
)
)
when comparing string you cannot use wildcards.Here you can see some examples about batch substrings
Here is a slightly different way of doing it.
#IF NOT EXIST "%USERPROFILE%\Desktop\Copy\" #EXIT/B
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "_path=%USERPROFILE%\Desktop\Copy"
SET "_index=0"
FOR /F "DELIMS=" %%A IN ('WHERE/R "%_path%" * 2^>NUL') DO (SET/A "_index+=1"
SET "_value[!_index!]=%%A")
IF %_index% EQU 0 EXIT/B
FOR /F "TOKENS=1* DELIMS==" %%A IN ('SET _value['
) DO IF /I "%%~xB"==".elf" ECHO %%B
PAUSE

How can we split string using windows bat

How can we split string using windows bat script?
for below .bat code snippet
#echo off & setlocal EnableDelayedExpansion
set j=0
for /f "delims=""" %%i in (config.ini) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a!
(echo !a!|findstr "^#">nul 2>nul && (
rem mkdir !a!
) || (
echo +)
rem for /f "tokens=2" %%k in(config.ini) do echo %%k
)
)
pause
below config file
Q
What's wrong when I del rem at the begin of rem for /f "tokens=2" %%k in(config.ini) do echo %%k
How can I get the /path/to/case and value as a pair?
for /f xxxx in (testconfig.ini) do (set a=/path/to/case1 set b=vaule1)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q43407067.txt"
set j=0
for /f "delims=""" %%i in (%filename1%) do (
set /a j+=1
set con!j!=%%i
call set a=%%con!j!%%
echo !a! SHOULD BE EQUAL TO %%i
(echo !a!|findstr "^#">nul 2>nul && (
echo mkdir !a!
) || (
echo +)
for /f "tokens=2" %%k IN ("%%i") do echo "%%k"
for /f "tokens=1,2" %%j IN ("%%i") do echo "%%j" and "%%k"
)
)
ECHO ----------------------------
SET con
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q43407067.txt containing your data for my testing.
(These are setting that suit my system)
SO - to address your problems:
because the ) on that line closes the ( on the previous. The ) on that line closes the ( on the one prior. (I changed the rem to an echo so that the code would produce something visible) The first ( on the (echo !a! line is closed by the ) on the line following the (now) two for /f commands. and the ( on the for..%%i..do( is closed by the final ) before the echo -----
You can't delete that ) because it's participating in a parenthesis-pair.
You need a space between the in and the (.
I've shown a way. See for /?|more from the prompt for documentation (or many articles here on SO)
In your code, !a! is the same as %%i - so I've no idea why you are conducting all the gymnastics - doubtless to present a minimal example showing the problem.
Note that since the default delimiters include Space then if any line contains a space in the /path/to/case or value then you'll have to re-engineer the approach.
I' not sure if I understand what exactly it is you need, so what follows may not suit your needs:
#Echo Off
SetLocal EnableDelayedExpansion
Set "n=0"
For /F "Delims=" %%A In (testConfig.ini) Do (Set "_=%%A"
If "!_:~,1!"=="#" (Set/A "n+=1", "i=0"
Echo=MD %%A
Set "con[!n!]!i!=%%A") Else (For /F "Tokens=1-2" %%B In ('Echo=%%A'
) Do (Set/A "i+=1"
Set "con[!n!]!i!=%%B"&&Set/A "i+=1"&&Set "con[!n!]!i!=%%C")))
Set con[
Timeout -1
GoTo :EOF
remove Echo= on line 6 if you are happy with the output and really want to create those directories

Batch File: FOR /F doesn't work as expected

I have an issue with reading lines from a *.txt file in batch script to get a list of files.
If my file contain something like
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme!!!.txt
2 errors.
and my batch is
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set count_to_sync=-1
for /f "tokens=*" %%i in (bubu.txt) do (
set line=%%i
echo %%i
if "!line:~0,9!" == "MD5 FAIL:" (
set /A count_to_sync+=1
set list[!count_to_sync!]=!line:~10!
)
if "!line:~0,20!" == "File does not exist:" (
set list[!count_to_sync!]=!line:~21!
set /A count_to_sync+=1
)
)
IF "%count_to_sync%" == "-1" (
ECHO Nothing to sync
) ELSE (
ECHO Files to sync
for /F "tokens=2 delims==" %%s in ('set list[') do (
echo %%s
)
)
The output is
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme.txt
2 errors.
Files to sync
release\devpath\readme.txt
release\mainline\readme.txt
and the '!!!' from second line is missing.
I know that if I remove SETLOCAL ENABLEDELAYEDEXPANSION from batch the output will be
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme!!!.txt
2 errors.
First part is OK, but the extraction will not work because delayed expansion is disabled.
How I can get the correct output?
Thank you
UPDATE
The input file with all types of lines
File does not exist: release\devpath\readme.txt
File does not exist: release\mainline\readme!!!.txt
MD5 FAIL: exf.exe
2 errors.
UPDATE
I need this script to sync changed files based on the output of 'exf.exe' used to check the integrity of folder based on md5 checksum
When the specifications of a problem are not described, but based on examples, we can make assumptions that may or may not be correct. My assumption is that you want the last token of the lines in your text file, so this is a possible (and much simpler) solution:
EDIT: I changed my original method for a simpler one.
#echo off
setlocal
set "msg=Nothing to sync"
(for /F "tokens=3,5" %%a in (bubu.txt) do (
set "msg=Files to sync"
if "%%b" neq "" (echo %%b) else echo %%a
)) > list.txt
echo %msg%
type list.txt
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q35477317.txt"
ECHO Files to sync
FOR /f "usebackqtokens=4*delims=: " %%a IN ("%filename1%") DO ECHO(%%b
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q35477317.txt containing your data for my testing.
It's not clear why you've taken your approach - is there something you haven't told us?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q35477317.txt"
ECHO Files to sync
FOR /f "usebackqtokens=1*delims=:" %%a IN ("%filename1%") DO (
FOR /f "tokens=*" %%c IN ("%%b") DO ECHO(%%c
)
GOTO :EOF
Easily fixed once we get the full story...
Maybe you need something like that:
#ECHO OFF
rem SETLOCAL ENABLEDELAYEDEXPANSION
set count_to_sync=-1
for /f "tokens=*" %%i in (bubu.txt) do (
set line=%%i
echo %%i
SETLOCAL ENABLEDELAYEDEXPANSION
if "!line:~0,9!" == "MD5 FAIL:" (
set /A count_to_sync+=1
set list[!count_to_sync!]=!line:~10!
)
if "!line:~0,20!" == "File does not exist:" (
set list[!count_to_sync!]=!line:~21!
set /A count_to_sync+=1
)
ENDLOCAL
)
IF "%count_to_sync%" == "-1" (
ECHO Nothing to sync
) ELSE (
ECHO Files to sync
for /F "tokens=2 delims==" %%s in ('set list[') do (
echo %%s
)
)

Find a string containing a substring in windows batch file

I have a textfile (filename.txt) which contains
ProductABC_Test.txt
ProductDEF_Test.txt
ProductHIG_Test.txt
ProductIJK_Test.txt
I will be getting a variable passed (ex: product=ABC which will be substring of ProductABC_Test.txt). So I need to fetch the correct test name (ProductABC_Test.txt) from the filename.txt.
I have tried the following code -
SETLOCAL ENABLEEXTENSIONS
#echo off
set product=ABC
SETLOCAL EnableDelayedExpansion
for /F "tokens=*" %%A in (filename.txt) do
(
set str=%%A
if NOT %str% == !%str:product=%
(
set test_suite=%%A
)
)
ENDLOCAL
echo %test_suite%
But I am not getting the right result.
You can use the following code to look for the line that contains your substring:
#echo off
SETLOCAL ENABLEEXTENSIONS
set product=ABC
set test_suite="not found"
SETLOCAL EnableDelayedExpansion
for /F "tokens=*" %%A in (filename.txt) do (
set str=%%A
Echo.!str! | findstr /C:"!product!">nul
if !errorlevel!==0 set test_suite=!str!
)
echo %test_suite%
pause

SubString replacement in batch files batch script

Hi i am trying to replace the contents of a text file using batchscript.
setlocal enableextensions enabledelayedexpansion
set line=0
set position=0
set replacetoken=POSITION
set newfile = new.txt
for /f "tokens=* delims=" %%x in (resproprty.txt) do (
#echo off
set /a line+=1
if not !line!==1 (
set /a position+=1
)
set newcount=0
set newcount=!position!
set content=%%x
echo !content!
set content=!content:%replacetoken%=POSITION#!position!!
echo !newcount!
echo !position!
#echo on
echo !content! >> NEW.TXT
)
set test=Hello!
echo %test%
endlocal
here i am trying to replace my token POSITION with POSITION#1 i.e POSITION#number which is being incremented. however its not working !position! is not giving any output, i even tried %position% but that just gives output as 0. please help
Try like this :
#echo off
if exist output.txt del output.txt
setlocal EnableDelayedExpansion
set /a $count=1
for /f "delims=" %%a in ('type "resproprty.txt"') do (
set line=%%a
call set line=!line:POSITION=POSITION#%%$count%%!
echo !line!>>output.txt
set /a $count+=1
)
Echo ok
That will create the file output.txt with the change done
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r "." q22988413.txt') DO (
SET "line=%%b"
SET "line=!line:position=position#%%a!"
ECHO(!line!
)
)>newfile.txt
GOTO :EOF
I used a file named q22988413.txt containing
Hello position one
Hello position two
Hello position three
Hello position four
Hello position five
which produced this output in newfile.txt
Hello position#1 one
Hello position#2 two
Hello position#3 three
Hello position#4 four
Hello position#5 five

Resources