I need copy specific line of one text file, then is file replaced with selected other file and substitute same line with copied text line. I have command line:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set mca=%cd%
cd /d %SYSTEMDRIVE%\PMJ\PROGRAMS\
set programs=%cd%
:select
echo.
echo Which file?
echo.
echo (without.PRG)
echo.
set /p load=
if exist %programs%\%load%.PRG (goto start) else (goto err1)
:err1
echo Not found
goto select
:start
for /F "tokens=*" %%B in (%mca%\mlfb.txt) do (
findstr /B /C:" InterlockingName = " %programs%\%%B > %mca%\actual.txt
set /p "actual= < %mca%\actual.txt"
copy /y %programs%\%load%.PRG %programs%\%%B
fart -a %programs%\%%B " InterlockingName = " %actual%
)
Searched line begins InterlockingName = then contains different characters.
File mlfb.txt contains
40AK11600.PRG
40AK11601.PRG
40AK11602.PRG
40AK11603.PRG
40AK11604.PRG
40AK11605.PRG
40AK11637.PRG
40AK11638.PRG
40AK11653.PRG
4OAK11609.PRG
5WK11706.PRG
A2C5330886804.PRG
A2C8171710004.PRG
Line
findstr /B /C:" InterlockingName = " %programs%\%%B > %mca%\actual.txt
creates file actual.txt, which contains string - for example
InterlockingName = 40AK11600
Command
copy /y %programs%\%load%.PRG %programs%\%%B
replace one file with selected file, but I can not to substitute line of replaced file with line in file actual.txt
fart -a %programs%\%%B " InterlockingName = " %actual%
(if file actual.txt is not needed, it can be eliminated).
It's working with command line modified (from :start point)
:start
for /F "tokens=*" %%B in (%mca%\mlfb.txt) do (
findstr /B /C:" InterlockingName = " %programs%\%%B>%mca%\actual.txt & set /p actual=<%mca%\actual.txt
copy /y %programs%\%load%.PRG %programs%\%%B
findstr /B /C:" InterlockingName = " %programs%\%%B>%mca%\before.txt & set /p before=<%mca%\before.txt
%fart% -a %programs%\%%B "!before!" "!actual!"
)
Related
I try to make text engine in cmd.
So I use this code:
#echo off
cls
echo h
cls
echo he
cls
echo hel
cls
echo hell
cls
echo hello
But when I run the file I can't see any text.
So I used another code:
#echo off
cls
echo h
goto :2
:2
cls
echo he
goto :3
:3
cls
echo hel
goto :4
:4
cls
echo hell
goto :5
:5
cls
echo hello
When I first open the file I only see the he. After that I see no text.
What did I did wrong?
I want to know why it has error and how to fix it to make text engine.
Here's an example of a windowsbatch-file which doesn't do what your example is demonstrating, but does something I think you may prefer instead:
#Set "TextString=This is my text string!"
#For /F %%G In ('"Prompt $H&For %%H In (1) Do Rem"') Do #Set "BS=%%G"
#(For /F Delims^=^ EOL^= %%G In ('(%SystemRoot%\System32\cmd.exe /D /S ^
/U /V /C "Echo=!TextString!"^) ^| %SystemRoot%\System32\find.exe /V ""'
) Do #Set /P "=.%BS%%%G" 0< NUL & %SystemRoot%\System32\PATHPING.EXE ^
127.0.0.1 -n -q 1 -p 150 1> NUL) & Echo=
#Pause
You simply place your required text to be printed, into the first line (between the = and closing "), and double-click it to run and view. If the intent is to run the script directly from cmd, you do not need the last line, #Pause, and can safely remove it.
As a result of a comment, I have decided to present the code in a slightly different way, so that it can be reused for different strings.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F %%G In ('"Prompt $H&For %%H In (1) Do Rem"') Do Set "BS=%%G"
Set "TextString=This is my text string!"
Call :GhostTypeIt
Set "TextString=This is "another" string."
Call :GhostTypeIt
Set "TextString=This & That are <words>."
Call :GhostTypeIt
Pause
Exit /B
:GhostTypeIt
(For /F Delims^=^ EOL^= %%G In ('(%SystemRoot%\System32\cmd.exe /D /S ^
/U /V /C "Echo=!TextString!"^) ^| %SystemRoot%\System32\find.exe /V ""'
) Do Set /P "=.%BS%%%G" 0< NUL & %SystemRoot%\System32\PATHPING.EXE ^
127.0.0.1 -n -q 1 -p 150 1> NUL) & Echo=
GoTo :EOF
I'm trying to loop through all .lss files in a folder, and grab a string that exists between two tags, save that value and rename the file using that string.
Example:
42982934829.lss -> contains string:
<surveyls_title><![CDATA[J.3200-1118 - Project Title]]></surveyls_title>
Rename to `J.3200-1118 - Project Title.lss`
Here is what I have so far, but I fear my syntax is badly incorrect..
#Echo off
Set Folder=X:\RenameTest
Set Files=*.lss
PushD %Folder%
For %%A in (%Files%) Do For /f %%B IN (
'findstr "<surveyls_title>.*</surveyls_title>" "%ProjectTitle%"'
) Do Call :Rename ..
PopD
Goto :Eof
:Rename
Echo Ren %1 "%ProjectTitle%"
I suppose this is what you want.
#echo off
pushd "X:\RenameTest"
for %%a in (*.lss) do for /f "tokens=3*delims=[]" %%i in ('type "%%a" ^| find "</surveyls_title>"') do echo ren "%%~a" "%%~i%%~xa"
popd
Just remove echo from the line once you are happy with the printed results.
You can also extract the title using Regex : Demo Here
#echo off & color 0A
Title Extract Title using Regex
Set "InputFile=42982934829.lss"
Call :Extract_Title "%InputFile%" Title
Echo %Title%
Pause & Exit
::----------------------------------------------------------------------------------------
:Extract_Title <InputFile> <Title to be Set>
(
echo WScript.StdOut.WriteLine Extract_Title(Data^)
echo Function Extract_Title(Data^)
echo Data = WScript.StdIn.ReadAll
echo Set re = New RegExp
echo re.Global = True
echo re.IgnoreCase = True
echo re.Pattern = "\[CDATA\[(.*?)\]\]"
echo For Each Match in re.Execute(Data^)
echo Title = Match.SubMatches(0^)
echo Next
echo Extract_Title = Title
echo End Function
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%A in ('cscript /nologo "%tmp%\%~n0.vbs" ^< "%~1"') do set "%2=%%A"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------------
I have the following file contents from which I want to extract the MD5 hash value thats on line 2.
How can I do this in a Windows batch file? Unfortunately using powerscript is not an option.
Herewith the input file ( file.txt )
MD5hashoffile20160613190010_Address
f4f855c5cb40767a7227b506f641ceef
CertUtil:-hashfilecommandcompletedsuccessfully.
I wanted to use the findstr utility but the regex I use must be wrong since it's not returning anything.
findstr /R "[a-fA-F0-9]{32}" file.txt
I appreciate any advice.
Thanks
UPDATE :
I have added the full solution in the answer section.
After some trial and error I found a solution that works :
findstr /R "^[a-fA-F0-9]*$" file.txt
Updated Answer with full solution :
SETLOCAL ENABLEDELAYEDEXPANSION
SET "FILESIZE=0"
SET "RECORDCOUNT=0"
SET "MD5HASH="
SET "TEMPHASH="
SET "FILESETREPORT=_UNSET"
SET "LINE=0"
SET "COLUMNS=TableName|RecordCount|FileSize|FileHash"
echo "============================="
echo "= Starting FileSize process ="
echo "============================="
for %%F in (*.csv) do (
echo "Process A CSV File"
for /f "tokens=1,2,3,4 delims=^_" %%A IN ("%%~nF") do (
SET "TABLENAME=%%D"
SET "FILESIZE=%%~zF"
if "!FILESETREPORT!" == "_UNSET" (
SET "FILESETREPORT=%%A_%%B_%%C_FilesetReport.csv"
:: Initialize the headers
echo !COLUMNS! > !FILESETREPORT!
)
)
echo "Get RecordCount in CSV"
if "!TABLENAME!" NEQ "FilesetReport" (
for /f "tokens=*" %%I in ('more +1 %%F ^| find /v /c ""') do SET "RECORDCOUNT=%%I"
echo "Generate File MD5 Hash"
certUtil -hashfile %%~nxF MD5 > hashfile
echo "Get the hash from the file"
for /f "skip=1 tokens=*" %%A in (hashfile) do (
SET "TEMPHASH=%%A"
:: call :cleanHash
if !LINE! == 0 SET "LINE=1" && call :cleanHash
)
:: Reset the Line Number
SET "LINE=0"
echo "Save File Details to FieldsetReport"
echo "RecordCount : !RECORDCOUNT!"
echo "FileSize : !FILESIZE!"
echo "MD5Hash : !MD5HASH!"
echo "TableName : !TABLENAME!"
SET "OUTPUTLINE=!TABLENAME!|!RECORDCOUNT!|!FILESIZE!|!MD5HASH!"
echo !OUTPUTLINE! >> !FILESETREPORT!
:: Cleanup
del hashfile
)
)
echo "File Processing Completed"
exit /b 0
:cleanHash
echo "Remove all spaces from the MD5 hash"
:: for /f "delims=" %%a in ('findstr /R "^[a-fA-F0-9]*$" !TEMPHASH!') do SET TEMPHASH=%%a
SET "MD5HASH=!TEMPHASH: =!"
echo "********************************************************************"
echo !MD5HASH!
ENDLOCAL
You're better off using this:
#echo off
for /f "skip=1" %%a in (file.txt) do set "hash=%%a" &goto breakLoop
:breakLoop
echo %hash%
pause
This should work even if another line also contains only hexadecimal characters, and has the added benefit of putting the md5 hash in a variable, ready to be used.
I am trying to create a batch file which will read from a file and store the text in a variable. Later this variable has to be replaced in another XML file as explained below
Loop thru the Inputfile by reading line by line i.e. one line after the other
Each time replace the file name that has been obtained from above step in payload.xml
Contine with step 2 until end of file is processed. Example input and output is given below
REM Example Input: D:\Data\somefile.txt
REM 1st Iteration Output: D:\Data\file1.txt
REM 2nd Iteration Output: D:\Data\file2.txt
REM and so on
I am trying to do the following but it is not working. Please kindly help.
SETLOCAL EnableDelayedExpansion
Set AllInputFile=D:\AllInputFile\FileList_for_Import.txt
SET INTEXTFILE=C:\myfolder\payload.xml
ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " %AllInputFile%1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt
ECHO "###########Start of MAIN OF LOOP ############"
for /f "tokens=*" %%a in (%AllInputFile%) do (
ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " line=%%a1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt
set "TARG_FILE=%%a"
ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " "TARG_FILE "%TARG_FILE%1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt
REM Get the string and store it in a variable
for /f "tokens=1*delims=:" %%G in ('findstr /n "^" C:\myfolder\payload.xml') do if %%G equ 2 set "DbgLine=%%H"
ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " "DbgLine "%DbgLine%1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt
SET PREVFILE_NM=%DbgLine:~27,-13%
ECHO %PREVFILE_NM%
call set NewDbgLine=%%DbgLine:!PREVFILE_NM!=!TARG_FILE!%%
echo !DbgLine!
ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " "NewDbgLine "!NewDbgLine!1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt
echo "#######################"
set SEARCHTEXT=%DbgLine%
echo "***************"
echo %SEARCHTEXT%
echo "***************"
set REPLACETEXT=%NewDbgLine%
set OUTPUTLINE=
REM Replace the file name
for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do (
SET string=%%A
for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
if "!string!" == "" (
echo.>>%OUTTEXTFILE%
) else (
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo %modified%
echo !modified! >> %OUTTEXTFILE%
)
)
ECHO "ERRORLEVEL 0-Sucesss else fail " %ERRORLEVEL% " " "End of Main Loop Iteration#######################"1>>D:\Data\debuginfo.txt2>>D:\Data\debugerr.txt
)
Best Regards,
Ind.
When using enabledelayedexpansion and changing variables in a loop
you need to use !variablename! syntax and not %variablename%
this batch script to search file better
using "find" its just ordinary...and need to type exactly the file name "file 1.txt" "file2.txt" "file letter.txt"
and exactly folder location...
any other way?
just try type "file"
its show all file with name "file"
"file 1.txt"
"file2.txt"
"file letter.txt"
and have number
1 . "file 1.txt"
2 . "file2.txt"
3 . "file letter.txt"
and enter the number to select the file what we want...
and it will be open.
#echo off
setlocal EnableDelayedExpansion
if exist log del log
set "token=%~1"
if not defined token (
echo Search for what string?
set /p token=^>
echo.
)
set a=0
for /f "delims=" %%A in ('dir /b ^| find "%token%"') do (
set /a a+=1
echo !a!. %%A
echo !a!. %%A >>log
)
echo.
echo Enter number of file to open.
set /p op=^>
for /f "tokens=1,2 delims=." %%A in (log) do (
if %%A EQU %op% start %%B
)
del log
the problem is
when enter the number...batch otomatic close...file wont open...
any suggestion?
you might try this:
#echo off &setlocal
set /p "spat=Enter search pattern: "
for /f "tokens=1*delims=:" %%a in ('dir /b /s /a-d \%spat%*.txt 2^>nul^|findstr /ri "%spat%[^a-z]"^|findstr /rn $') do set "$%%a=%%~b"&set /a fcnt+=1
if not defined fcnt (echo Error! Not such file "%spat%".&goto:eof)
:loop
for /f "tokens=1*delims==$" %%a in ('set "$" 2^>nul') do echo %%a.%%~b
set /p "fno=Enter file number to run: "
echo %fno%|findstr "[1-9][0-9]*" >nul || (echo Error! Try again.&goto:loop)
if %fno% gtr %fcnt% (echo Error! Enter number between 1-%fcnt%. Try again.&goto:loop)
for /f "tokens=1*delims==$" %%a in ('set "$%fno%"') do "%%~b"