I wrote a very simple script to output the host machine's MAC addresses to a text file.
The script is exiting right after line 3 - 'IF DEFINED WRITEOK ('.
#echo off
cls
copy /Y NUL "%CD%\.writable" > NUL 2>&1 && set WRITEOK=1
IF DEFINED WRITEOK (
rem ---- we have write access ----
set DIR=%CD%\interfaces
set FILE=%DIR%\%USERNAME%.txt
IF NOT EXIST "%DIR%" (
MKDIR "%DIR%"
echo DIR '%DIR%' was created
) else (
echo DIR '%DIR%' already exists
) for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (
echo %%i >> "%FILE%"
echo OUTPUT written to '%FILE%'
)
) else (
rem ---- we don't ----
echo DIR '%DIR%' is not writable
)
echo.
echo DONE!
pause
Try to put the FOR one line after the closing parenthesis :
...)
for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (...
you can't start a FOR with a closing parenthesis in front :
This will not work :
(echo 1
) for /l %%a in (1,1,10) do echo %%a
and this will work :
(echo 1
)
for /l %%a in (1,1,10) do echo %%a
EDIT 1 :
For the path variables containing space use double quote :
"%cd%"
when using it.
I have this following code.
#echo off
setlocal EnableDelayedExpansion
set holdingline=,Measure,,+ X,,,0,0
FOR /F "tokens=1,2,3,4,5,6,7,8 delims=," %%a IN ("%holdingline%") DO (
echo %%a
echo %%b
echo %%c
echo %%d
echo %%e
echo %%f
echo %%g
echo %%h
echo %holdingline%
)
pause
Output displayed is as below:
Measure
+ X
0
0
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
,Measure,,+ X,,,0,0
The empty strings are pushed to the end and I wonder why. I am expecting them in order, say something like:
ECHO is off.
Measure
ECHO is off.
+ X
ECHO is off.
ECHO is off.
0
0
,Measure,,+ X,,,0,0
This would enable me to assign them to the correct variables. I tried searching but did not find much help.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set holdingline=,Measure,,+ X,,,0,0
FOR /F "tokens=1-8 delims=," %%a IN (""%holdingline:,=","%"") DO (
ECHO(%%~a
ECHO(%%~b
ECHO(%%~c
ECHO(%%~d
ECHO(%%~e
ECHO(%%~f
ECHO(%%~g
ECHO(%%~h
echo %holdingline%
)
GOTO :EOF
This should solve your problem
To process a file, producing a new file
#ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%x IN (q29639243.txt) DO (
set "holdingline=%%x"
CALL :process
)
)>u:\new.txt
GOTO :EOF
:process
FOR /F "tokens=1-8 delims=," %%a IN (""%holdingline:,=","%"") DO (
ECHO(%%~a
ECHO(%%~b
ECHO(%%~c
ECHO(%%~d
ECHO(%%~e
ECHO(%%~f
ECHO(%%~g
ECHO(%%~h
echo %holdingline%
)
GOTO :EOF
I used a file named q29639243.txt containing similar data for my testing.
Produces u:\new.txt
This should do what you want it to:
#echo off
setlocal EnableDelayedExpansion
set holdingline=,Measure,,+ X,,,0,0
FOR /F "tokens=1,2,3,4,5,6,7,8 delims=," %%a IN ("%holdingline:,,=, ,%") DO (
echo %%a
echo %%b
echo %%c
echo %%d
echo %%e
echo %%f
echo %%g
echo %%h
echo %holdingline%
)
pause
Replacing ,, with , , will cause it to set the middle variables to (space) and hence treat them like nothing. If you don't have anything between two , it will skip them and hence only 4 variables exist, leaving e-h blank
I need to read a file in outer loop line by line, take this value and use it in inner loop. But currently I am able to read first line from this file and do some required processing in inner loop but outer loop runs only once.
Why does the outer loop run only once?
myfile.txt contains:
AWC00201
AWC00202
AWC00203
DDDD
#echo off
setlocal EnableDelayedExpansion
for /F %%D in (myfile.txt) do (
echo %D%
S:
cd \#vantage\AFG\AWC\AWCU\simulation\WRO_Regression_results\%%D
echo %%D
FOR /F %%i IN ('dir /b /ad-h /o-d') DO (
echo After Nested For
echo %%D
SET test=%%D
SET b=%%i
GOTO found
)
echo No subfolder found
goto done
:found
echo %D%
echo Most recent subfolder: %b%
cd %b%
echo %%D
find /c "O K" tooling.report
echo %D%
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo notfound
goto done
:done
echo %D%
echo now go up
echo !test!
echo %test%
)
pause
I am getting following output:
ECHO is off.
AWC00201
After Nested For
AWC00201
ECHO is off.
Most recent subfolder: 20141103_170658_wro_awc
%D
____________ TOOLING.REPORT: 0
ECHO is off.
notfound
ECHO is off.
now go up
AWC00201
AWC00201
Press any key to continue . . .
Your code has one big problem and one thing to change
The problem is that it is not possible to use goto while inside a for loop and keep the loop iterating. goto cancels the for looping.
The thing to change is your use of variables. You have the information you need inside the for replaceable parameters. Use them. Move the value to a variable when the replaceable parameters does not offer what you need, but this is not the case
#echo off
setlocal enableextensions disabledelayedexpansion
for /F "delims=" %%D in (myfile.txt) do (
cd /d "s:\#vantage\AFG\AWC\AWCU\simulation\WRO_Regression_results\%%D"
for /d %%a in (.) do echo Current folder is "%%~fa"
set "file="
FOR /F "delims=" %%i IN ('dir /b /ad-h /o-d 2 >nul ') DO if not defined file (
set "file=1"
echo subfolder found : %%i
find /c "O K" ".\%%i\tooling.report" >nul 2>nul
if errorlevel 1 (
echo O K found
) else (
echo O K not found or file does not exist
)
)
if not defined file (
echo subfolder not found
)
)
pause
I am trying to replace a line in load.xml using the lines read from FileList.txt.
Contents of load.xml
<mainheader>
<InFilePath>D:\Data\All_Inputfiles\oldfile.txt</InFilePath>
</mainheader>
FileList.txt
newfile1.txt
newfile2.txt
Expecting the output with each iteration as
<mainheader>
<InFilePath>D:\Data\All_Inputfiles\newfile1.txt</InFilePath>
</mainheader>
and with the next iteration replace newfile1.txt with newfile2.txt. I am able to get original string with the final string but last part of the code is throwing syntax error, i.e. from
for /f "delims=" %%a in (!INTEXTFILE!) do call :Change "%%a" .
Could please help me?
Thanks in advance.
#echo off
SETLOCAL EnableDelayedExpansion
Set AllInputFile= D:\data\FileList.txt
SET INTEXTFILE=C:\c:\load.xml
set OUTTEXTFILE=D:\data\tmp_out.txt
SET BackupPath=D:\data\backupload.xml
Set TempFile=D:\data\tmp.txt
SET DbgFile=D:\data\debuginfo.txt
Del !TempFile!
Del !DbgFile!
Copy !INTEXTFILE! !BackupPath!
:replace
findstr /g "InFilePath" !INTEXTFILE!>!TempFile!
:: set string=" <InFilePath>D:\Data\All_Inputfiles\oldfile.txt</InFilePath>"
set /p string=< !TempFile!
SET PREVFILE_NM=!string:~75,-13!
set FinalreplaceLine=!string!
set TARG_FILE=%~1
REM ECHO "Before Replace FinalreplaceLine "!FinalreplaceLine!>>!DbgFile!
set FinalreplaceLine=!FinalreplaceLine:%PREVFILE_NM%=%TARG_FILE%!
ECHO "string "!string!>>!DbgFile!
ECHO "FinalreplaceLine "!FinalreplaceLine!>>!DbgFile!
::string has source string and FinalreplaceLine has target string to replace
for /f "delims=" %%a in (!INTEXTFILE!) do call :Change "%%a"
exit /b
:Change
set Text=%~1
if "%Text%"=="%string%" (
(echo !FinalreplaceLine!)>> !OUTTEXTFILE!
) else (
(echo !Text!)>> !OUTTEXTFILE!
)
exit /b
Does this work for you?:
#echo off
for /f "usebackq delims=" %%a in ("FileList.txt") do (
call :change "%%a"
type "load.xml"
pause
)
echo done
pause
goto :EOF
:change
(
echo ^<mainheader^>
echo ^<InFilePath^>D:\Data\All_Inputfiles\%~1^</InFilePath^>
echo ^</mainheader^>
) > "load.xml"
I wrote a batch but it doesnt work at all:
set THEFILE=ip_list.txt
if exist result.txt del result.txt
::read %THEFILE% and loop through each line
for /F %%A in (%THEFILE%) do (
SETLOCAL ENABLEDELAYEDEXPANSION
SET counter=1
FOR /F "tokens=* USEBACKQ" %%F IN (`ping %%A`) DO (
SET result%counter%=%%F
SET counter=%counter%+1
)
ECHO %result1%
ECHO %result2%
ECHO %result3%
echo %result9% >> result.txt
ENDLOCAL
echo ******************************************* >> result.txt
)
inside of ip_list.txt :
8.8.8.8
4.4.4.4
I need to get ip adresses if not %100 loss occurs..
I need a quick help in my code..
Here is how I'd do it:
#echo off
setlocal
set THEFILE=ip_list.txt
if exist result.txt del result.txt
for /F %%A in (%THEFILE%) do (
call :IsPingable %%A && (
Echo %%A is pingable ) || (Echo %%A is not pingable)
)
exit /b
:IsPingable <comp>
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b