Why don't I get the correct number of lines from .txt and also random number does not change - batch-file

two things I don't understand what I can do more.
%%C instead of showing me that in output-mp3.txt (%%B) I have one single line -> the output is 124000 lines. If I make the for /f %%C alone in another .bat is showing me the corect number of lines.
I need if %%C == 1 to get !exportname! (%%~nB) which is the name of the song and if is not equal with 1 to be generated a random name. My conditional command doesn't work also %RANDOM% show all the time the same number when is looped.
If someone have a more professional and optimized approach of what I need I will very gladly to hear any directions.
#echo off
Setlocal EnableDelayedExpansion
for /F "delims=" %%A in (mp3list-1.txt) DO (
for /F "tokens=2 delims='" %%B in (%%A) DO (
for /f %%C in ('Find /V /C "" ^< %%B') do (
echo %%A
echo %%B
echo %%~nB
echo %%C
Echo Notepad file has %%C lines and the name of the song is %%~nB
if "%%C"=="1" (
set /p exportname=%%~nB
Echo Name of the song
) else (
set /p exportname=!RANDOM!
Echo Random number
)
Echo !exportname!
)
)
)
mp3list-1.txt contain
F:\.....\Videos-1\output-mp3.txt
output-mp3.txt in this case %%B contain
file 'F:\....\Canal-1\Videos-1\name of the song.mp3'
LATER EDIT:
I tried like this also with the third FOR, but same not showing me that I have 1 single line .. batch tell me I have 50k lines, witch is no true... also changed make some other changes.. no luck ..
#echo off
Setlocal EnableDelayedExpansion
for /F "UseBackQ delims=" %%A in ("mp3list-1.txt") DO (
for /F "UseBackQ tokens=2 delims='" %%B in ("%%A") DO (
set "cmd=findstr /R /N "^^" "%%B" | find /C ":""
for /f %%C in ('!cmd!') DO (
echo %%A
echo %%B
echo %%~nB
echo %%C
Echo Notepad file has %%C lines and the name of the song is %%~nB
if "%%C"=="1" (
set /p exportname=%%~nB
Echo Name of the song
) else (
set /p exportname=!RANDOM!
Echo Random number
)
Echo !exportname!
)
)
)

Because your script and comments/questions do not match I have made some guesses / assumptions.
This is what your script does:
%%A is a line from file 'mp3list-1.txt'
%%B is the full path and filename extracted from %%a, this is an mp3 file
%%C is the number of lines in the mp3 file! (this is a binary file not text).
Your comments mention a file named 'output-mp3.txt'.
Just for clarity I have added the file output-mp3.txt to keep the changes close to your code.
But the output-mp3.txt is not needed.
Also the check for number of lines is not needed because this file will have just 1 line (%%B).
Because this mystery file "output-mp3.txt" is neither created, nor used in your code this is a bit of a guessing game.
#echo off
Setlocal EnableDelayedExpansion
for /F "delims=" %%A in (mp3list-1.txt) DO (
for /F "tokens=2 delims='" %%B in (%%A) DO (
> "output-mp3.txt" echo %%B
rem type "output-mp3.txt"
rem for /f %%C in ('Find /V /C "" ^< %%B') do (
for /f %%C in ('Find /V /C "" ^< "output-mp3.txt"') do (
echo %%A
echo %%B
echo %%~nB
echo %%C
Echo Notepad file has %%C lines and the name of the song is %%~nB **
if "%%C"=="1" (
set /p exportname=%%~nB
Echo Name of the song
) else (
set /p exportname=!RANDOM!
Echo Random number
)
Echo !exportname!
)
)
)

Related

How to add a goto to a for loop

I am working on a script that edits files and I am building a choice goto menu.
The script itself works fine, it reads from a file name that has ( ) in it. So my files are saved like this (1) filename.txt, (2) filename.txt ....(100) filename.txt
the content of the file has "times": 125489, saved in it, the script removes the 100 from the file name and does this "times": 100,
This is a small part of my content for my txt files it has a 4 indent space per line I believe the format is Json
"aidr": 3.58,
"nlpr": 0.5,
"tafr": 0.5,
"titp": 0.5,
"trld": 0.0,
"tssp": 0.5,
"tssr": 0.5,
"ttup": 0.5,
"ttpp": 0.5,
"times": 125,
"Stamp": 125,
"ppiid": 649,
"otiid": 649,
"apcid": 9,
"orcid": 9,
"jpcns": 0,
"agpns": 0,
"opcns": 0,
"rppns": 0,
I recently found that when I merged all my single scripts together it slows down dramatically. Before the script processed 500 .txt files in 1 minute but now it takes 30 minutes.
My goal is to find a way to bring back the speed of this script.
I heard and read that adding a goto before and after the loop could make the script fast again.
#echo off
:Menu
ECHO ################################################################
echo.
ECHO 1 - Script 1
ECHO 2 - Script 2
ECHO 3 - Script 3
ECHO 4 - Script 4
ECHO 5 - Script 5
ECHO 100 - Script 100
echo.
set pass=
:: the choice command
set /p Mchoice=Make Your Choice:
::goto choices
goto=:%Mchoice%
goto %goto%
:1
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
set "ToReplace1="times": "
SET "sourcedir=New folder 1"
SET "destdir=New fodler 2"
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq "!line:times=!" CALL :Nums1 %%j
echo(!line!
)
)
)>"%destdir%\%%q"
)
GOTO :Menu
:Nums1
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace1%%%s=%ToReplace1%!"
IF "%original%" neq "%line%" goto Nums1
set "line=!line:%ToReplace1%=%ToReplace1%%1!"
GOTO :eof
:2
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
set "ToReplace2="Stamp": "
SET "sourcedir=New folder 1"
SET "destdir=New fodler 2"
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq "!line:Stamp=!" CALL :Nums2 %%j
echo(!line!
)
)
)>"%destdir%\%%q"
)
GOTO :Menu
:Nums2
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace2%%%s=%ToReplace2%!"
IF "%original%" neq "%line%" goto Nums1
set "line=!line:%ToReplace2%=%ToReplace2%%1!"
GOTO :eof
:15
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
cd /d "%~dp0"
set "ToReplace1="times": "
set "ToReplace2="Stamp": "
set "ToReplace3="jpcns": "
SET "sourcedir15=New folder 1"
SET "destdir15=New fodler 2"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%sourcedir15%\(*)*.txt"') DO (
SET "newdest=%%~dpq"
SET "newdest=!newdest:%sourcedir15%=%destdir15%!"
SET "newdest=!newdest:~0,-1!"
MD "!newdest!" 2>nul
(
FOR /f "tokens=1 delims=()" %%j IN ("%%~nxq") DO (
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%%q"') do (
set "line=%%b"
if defined line IF "%%b" neq "!line:times=!" CALL :Nums15 %%j
if defined line IF "%%b" neq "!line:Stamp=!" CALL :Nums15 %%j
if defined line IF "%%b" neq "!line:jpcns=!" CALL :Nums15 %%j
echo(!line!
)
)
)>"!newdest!\%%~nxq"
)
GOTO :Menu
:Nums15
SET "original=%line%"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace1%%%s=%ToReplace1%!"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace2%%%s=%ToReplace2%!"
FOR /L %%s IN (0,1,9) DO set "line=!line:%ToReplace3%%%s=%ToReplace3%!"
IF "%original%" neq "%line%" goto Nums15
set "line=!line:%ToReplace1%=%ToReplace1%%1!"
set "line=!line:%ToReplace2%=%ToReplace2%%1!"
set "line=!line:%ToReplace3%=%ToReplace3%%1!"
GOTO :eof
I've done a few exercises on this problem, but I've been unable to locate anything that might result in a 30:1 speed difference.
This version
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:times=!" (ECHO %%c) ELSE (
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: %%j,
)
) ELSE ECHO(
)
)
)>"%destdir%\%%q"
)
Seemed to be a little faster.
This:
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
SET "skipover="
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%sourcedir%\%%q"') do (
IF DEFINED skipover (ECHO(%%b
) ELSE (
set "line=%%b"
if defined line IF "%%b" neq "!line:times=!" SET "skipover=y"&CALL :Nums1 %%j
echo(!line!
)
)
)
)>"%destdir%\%%q"
)
uses a Boolean variable skipover to bypass some of the processing once the target line has been found and manipulated - but it actually seemed slower.
This
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:times=!" (ECHO %%c) ELSE (
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: %%j,
)
) ELSE ECHO(
)
)
)>"%destdir%\%%q"
)
was consistently a little faster than the original - it avoids the subroutine call.
And this:
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO (
SET "skipover="
for /F "usebackqdelims=" %%e in ("%sourcedir%\%%q") do (
IF DEFINED skipover (ECHO/%%e
) ELSE (
set "line=%%e"
if defined line (
IF "%%e" equ "!line:times=!" (ECHO %%e
) ELSE (
SET "skipover=Y"
for /F "tokens=1* delims=:" %%v in ("%%e") do ECHO %%v: %%j,
)
) ELSE ECHO/
)
)
)
)>"%destdir%\%%q"
)
was consistently nearly twice as fast, using the skip-if-it's been-changed technique and avoiding findstr. It won't reproduce empty lines, though.
I also found that the position of the target in the source was signiificant. Using a 700-line version of the sample data, placing the target at the end was half-speed of placing it at the start of the file, so I was able to get nearly a 4:1 speed difference between the original and this last technique if the target was placed early in the source data (running 1,000 700-line files).
I presume that sub appeared in the original post by way of editing-out the irrelevant portions of the code, but it seemed to be reversed in sense, the way it was written...
--- based on version 3,
:: remove variables starting #
FOR /F "delims==" %%e In ('set # 2^>Nul') DO SET "%%e="
:: Read controls from file "%1"
FOR /f "usebackqtokens=1*" %%b IN ("%~1") DO (
SET "#%%b=%%c"
FINDSTR /x /i /L /c:":#%%b" <"%~f0" >NUL
IF ERRORLEVEL 1 ECHO LABEL ":#%%b" NOT found&SET "destdir="
)
IF NOT DEFINED destdir ECHO Required LABEL(s) NOT found &GOTO :eof
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\(*)*.txt"') DO (
(
rem %%q contains the name of the file to be processed
rem remove variables starting $
FOR /F "delims==" %%e In ('set $ 2^>Nul') DO SET "%%e="
rem extract filenumber if sourcefilename is (number)something
SET "filenumber="
FOR /f "tokens=1 delims=()" %%j IN ("%%q") DO SET "filenumber=%%j"
rem
rem now process the file
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
rem if any #name is detected, take action
SET "processline=Y"
FOR /f "tokens=1,2delims=#=" %%u IN ('set #') DO IF DEFINED processline IF "%%c" neq "!line:%%u=!" (
SET "processline="
CALL :#%%u %%v
)
IF DEFINED processline ECHO %%c
) ELSE ECHO/
)
)>"%destdir%\%%q"
)
GOTO :eof
:#times
rem replace times value with filenumber
ECHO "times": %filenumber%,
GOTO :eof
:#titp
rem replace titp value with value read from file (%1)
ECHO "titp": %1,
GOTO :eof
:#tssp
rem put newz value before and zwen value after tssp value
ECHO "newz": whatever,
ECHO %line%
ECHO "zwen": revetahw,
GOTO :eof
:#trld
rem delete all trld values
GOTO :eof
:#tssr
rem replace the third and the fifth tssr value (I have multiples)
rem note that all $ variables are deleted FOR each file
SET /a $#tssr+=1
IF %$#tssr%==3 ECHO "tssr": thirdtime,&GOTO :eof
IF %$#tssr%==5 ECHO "tssr": fifthtime,&GOTO :eof
ECHO %line%
GOTO :eof
Using a control file contents
times #
titp jello
trld #
tssp #
tssr #
So - this is an attempt to obviate the repeated cloning of the file-processing logic.
First, I'm setting all variables that start # to nothing, un-defining them. That way, the remainder of the code knows that any variable starting # has not been inherited from elsewhere.
Next step : read the file to set up
#times=#
#titp=jello
#trld=#
#tssp=#
#tssr=#
The code then examines itself to make sure that subroutines :#times etc all exist and complains if they don't. destdir is simply a convenient non-empty variable used as a flag - we'll abort if the subroutines are missing, so no problem there.
Next - read the filenames. Obviously, the filemask may change depending on the selection made from the menu.
Then, for each file, kill all $ variables for the same reason as # but this time for each file to be processed.
Then set up variables to contain whatever data about the file is appropriate. Could be size, date, whatever - extract-parenethesised-number is shown. No harm in doing such things for all files, even if the data is not used for this particular menu selection.
Then process the file, detecting the keystrings from the # array. Call :#keystring if found, regurgitate line if not.
Each :# routine does what it does. A variety of possibilities is shown. Naturally, any particular routine could be controlled further by testing the selection made.
Setting the value assigned to #? to a value other than # will deliver that value to the :#? subroutine as parameters. Use them as you will - constants, whatever - just interpret the parameters delivered.
Naturally, you could set the # variables manually for particular menu selections instead of reading them from a file.
But - this is drifting way away from the timing problem - just adding more facilities (I'm a chronic implementor of bells and whistles) so I'd suggest to end discussion here.

Batch to get lines between two strings in multiple text files in subfolders

I have a series of text files each named the same in sub-folders of a certain directory
ac.txt files have the following structure :
---
some text
---
[lights]
---
some text
---
[GetEngineData]
---
some text
---
I want to get all those lines in between strings [lights] and [GetEngineData] (including those start [lights] and end [GetEngineData] lines) in one single output file called lights.txt with a blank space in between those coming from each text file.
I coded the following batch yet it is of no avail so far :
#ECHO OFF
for /r %%a in ('find /n "[lights]"^<(ac.txt) ') do set /a start=%%a
for /r %%a in ('find /n "[GeneralEngineData]"^<(ac.txt) ') do set /a end=%%a
(
for /r %%a in ('find /n /v ""^<(ac.txt) ') do (
IF %%a geq %start% IF %%f leq %end% ECHO(%%b
)
)>lights.txt
Here's a way to do it. Might not be the most efficient but it seems to do the job just fine. The code loops through all subfolders and picks up all .TXT files. It then parses each line of each file, marking the beginning/end of each block using the [lights] and [GeneralEngineData] tokens and then outputs everything to res.txt in the same folder where the batch file is stored.
#ECHO OFF
Setlocal EnableDelayedExpansion
if exist res.txt del res.txt
set inblock=0
for /r . %%a in (*.txt) do (
set fname=%%a
for /f "tokens=1* delims=]" %%b in ('type "!fname!" ^| find /n /v ""') do (
if /i *%%c*==*[lights]* set inblock=1
if !inblock!==1 (
if *%%c*==** (echo.) else (echo %%c)
if /i *%%c*==*[GetEngineData]* set inblock=0
)
)
echo.
) >> res.txt
set fname=
set inblock=
type res.txt

Bat file that takes specific variables on specific lines from texts

I need to create a batch file that searches through 2 texts. Captures a line of text in a variable (that contains at least one of 3 strings, but doesn't contain forth string) and its line number.
Searches through the second text and captures in another variable the line of text that exists on the line-number of the first variable.
I need to use the two lines-of-text (variable) after that as well.
I kind of managed through the first text reading, but not sure what I'm doing wrong in the second one:
#echo off
set "found="
for /f "tokens=1,* delims=[]" %%a in (' find /n /v "" ^< "%LocalDir%\list.txt" ') do (
echo "%%b"|findstr /i /c:"one two small" /c:"one two birds" /c:"one two strings" >nul && set found=1
if defined found echo "%%b"|findstr /v /c:"one two small red apples" >nul || set "found="
if defined found (
echo %%a found
#echo off & setLocal EnableDelayedExpansion
set var=%%b
set Line_num=%%a
endlocal
) else (
echo %%a NOT FOUND
)
set "found="
)
REM part2--------------------
for /f "delims=" %%d in (list1.txt) do (
set FullVersion=%%d
#echo off & setLocal EnableDelayedExpansion
for /f "tokens=1* delims=" %%e in ("%%d") do (
if !Line_num!==%%e
set var2=!FullVersion!
echo !var2!
)
)
endlocal
echo %var%
echo %var2%
Any help will be appreciated.
here is what I end up with as solution:
for /f "tokens=1,* delims=[]" %%a in (' find /n /v "" ^< "%LocalDir%\software_list.txt" ') do (
echo "%%b"|findstr /i /c:"Micro Focus Enterprise " /c:"Micro Focus Visual" /c:"Micro Focus COBOL" >nul && set found=1
if defined found echo "%%b"|findstr /v /c:"Micro Focus Enterprise Server for .NET" >nul || set "found="
if defined found (set LineNumber=%%a&set ProductName=%%b)
REM else (echo Main Micro Focus product NOT FOUND. Nothing to do. Exit.&exit /b)
set "found="
)
find "2." temp1.txt > temp3.txt
for /f "tokens=2,3 delims==" %%c in (temp3.txt) do (echo %%c >> %LocalDir%\software_list1.txt)
for /f "tokens=1*delims=[]" %%a in (' find /n /v "" ^< "software_list1.txt" ') do IF %%a==%LineNumber% SET ProductVersion=%%b
REM ECHO %LineNumber%
REM ECHO %ProductName%
REM ECHO %ProductVersion%
set ProductName=%ProductName:"=%
set ProductName=%ProductName: =%
set ProductVersion=%ProductVersion:"=%
set ProductVersion=%ProductVersion: =%
set out_file_name=%ProductName%_%ProductVersion%_%COMPUTER_NAME%
REM echo %out_file_name:"=%
Thanks a lot to everyone.
I see some problems in your code:
This block makes no sense, as it set variables in a new setlocal context and after the endlocal the variables are lost.
#echo off & setLocal EnableDelayedExpansion
set var=%%b
set Line_num=%%a
endlocal
In the second block you open a setlocal context for each iteration, that will result in a overflow error.
And the endlocal after the Part2 seems also to be contraproductive.
The line if !Line_num!==%%e creates always a syntax error
Btw. Why do you use #echo off inside your code? The frist one at the batch start should be enough.
You should only use one setlocal EnableDelayedExpansion at the beginning of the script.
You should only use DelayedExpansion toggling, if you have problems with exclamation marks.
You could use some echo's to see what happens, like
for /f "tokens=1* delims=" %%e in ("%%d") do (
echo Compare: !Line_num!==%%e
if !Line_num!==%%e set var2=!FullVersion!
echo !var2!
)
#echo off
set "found="
for /f "tokens=1*delims=[]" %%a in (
' find /n /v "one two small red apples" ^< "%LocalDir%\list.txt" ') do (
echo "%%b"|findstr /i /c:"one two small" /c:"one two birds" /c:"one two strings" >NUL
IF NOT ERRORLEVEL 1 SET lnbr=%%a&SET ltext=%%b
)
for /f "tokens=1*delims=[]" %%a in (' find /n /v "" ^< "list1.txt" ') do IF %%a==%lnbr% SET L1txt=%%b
ECHO(line number %lnbr%
ECHO(from LIST %ltext%
ECHO(from LIST1 %L1txt%
This should do what you want - if I understand correctly. Much better to show your data and an example of required output. Trying to fix code that DOESN'T do something undefined is frustrating.
#echo off
rem I need to create a batch file that searches through 2 text FILEs.
rem Captures a line of text in a variable (that contains at least one of 3
rem strings, but doesn't contain forth string) and its line number.
set Line_num=
for /F "tokens=1* delims=:" %%a in (
'findstr /N /I /C:"one two small" /C:"one two birds" /C:"one two strings" "%LocalDir%\list.txt"
^| findstr /V /C:"one two small red apples"' ) do (
echo %%a found
set var=%%b
set Line_num=%%a
)
REM part2--------------------
if defined Line_num (
rem Searches through the second text and captures in another variable
rem the line of text that exists on the line-number of the first variable.
for /F "tokens=1* delims=:" %%d in ('findstr /N "^" list1.txt') do (
if %Line_num% == %%d (
set var2=%%e
echo %%e
)
)
)
echo %var%
echo %var2%

How to get a Substring from list of file names

I want to develop the following logic
Read all files in a directory
Extract the first part of the filename – this will be the partner name
Extract anything after the first underscore- this will be filename
Eg: ZZTEST_123_abc_doc.txt  ZZTEST is partner. 123_abc_doc.txt is the filename.
Below is the code I developed
#echo off
setlocal ENABLEDELAYEDEXPANSION
Set Test_Dir=C:\Axway\projects\Cardinal\dosscript\test
cd %Test_Dir%
for /r %%a in (*.*) do (
Set "fname1=%%~nxa"
echo Filename is :!fname1!
for /f "tokens=1 delims=_" %%i in ("!fname1!") do (
Set "partner=%%i"
echo Partner is :!partner!
Set "str_tmp=!partner!_"
echo !str_tmp!
call :strlength length !str_tmp!
echo !length!
set fname=!fname1:~%length%!
echo !fname1:~%length%!
)
)
goto :eof
:strlength
setlocal enableextensions
set "#=%~2"
set length=0
:stringLengthLoop
if defined # (set "#=%#:~1%"&set /A length+=1&goto stringLengthLoop)
endlocal && set "%~1=%length%"
GOTO :EOF
But the result is
ID_ZZRoutingID_filename.txt
Filename is :ZZRoutingID_ZZRoutingID_filename1.txt
Partner is :ZZRoutingID
12
Result: ID_ZZRoutingID_filename1.txt
The result should be ZZRoutingID_filename1.txt but i am getting
ID_ZZRoutingID_filename1.txt.
Please help
The purpose of the length calculation is not clear to me, but I would suggest adding an asterisk following the 1 in your for /f "tokens=1 delims=_". You would then get the "filename" you were looking for through %%j.
I tested it like this:
#echo off
setlocal EnableDelayedExpansion
set source=D:\Program Files\Somewhere
cd %source%
for /r %%i in (*.*) do (
for /f "tokens=1* delims=_" %%j in ( "%%~nxi" ) do (
echo partner: %%j
echo name: %%k
)
)
endlocal
If you do not need to recurse through sub-directories:
#echo off
set source=D:\Program Files\Somewhere
for /f "tokens=1* delims=_" %%i in ( 'dir "%source%" /b /a-d' ) do (
echo partner: %%i
echo filename: %%j
)
dir /b /a-d retrieves the list of a directory's content except its sub-directories:
D:\Program Files\Somewhere>dir /b /a-d
ZZTEST_123_456.txt
ABCDEF_890_FFF.doc
FOOBAR_567_###.zzz

How do I only assign FOR /F token from one line only in a test file?

How do I make this grab the token from the first line ONLY in .txt file instead of looping through every line. I want %%m to be assigned to the 3rd token on line one only then stop.
#echo off
FOR %%A IN (.\xdrive\*.txt) DO (
FOR /F "usebackq tokens=3 delims=," %%m IN ("%%A") DO (
IF "%%m" == "F01" (xcopy /Y "%%A" .\Outbound)
pause
)
)
pause
set /p can be used to read the first line, and then you can use a FOR /F loop to get the third token
setlocal EnableDelayedExpansion
FOR %%A IN (%1) DO (
<%%A set /p firstline=
FOR /F "tokens=3 delims=," %%m IN ("!firstline!") DO (
echo %%m
)
)
Without see the eg files and knowing exactly what you're trying to do I can't test this, but here's the listing of firstline.bat which should do what you're asking for :) At first I thought this needed to be more complicated than it is... after your first if simply use a goto to exit the for structure after it's first call - problem solved?
#echo off
::: firstline.bat - Retrieve the first line from a series of files
::: usage: firstline $filespec
::: filespace - files to process (eg .\xdrive\*.txt)
if "%~1"=="" findstr "^:::" "%~f0"&GOTO:EOF
FOR %%A IN (%1) DO (
call :testfirst "%%A"
)
goto :eof
:testfirst
FOR /F "usebackq tokens=3 delims=," %%m IN (%1) DO (
IF "%%m" == "F01" (xcopy /Y %1 .\Outbound)
goto:eof
)
See this post, which shows how to mimic the gnu head utility using a dos batch file:
Windows batch command(s) to read first line from text file
untested
read first line tokens3
for /f "tokens=3 delims=," %%a in ('"findstr /n . %1|findstr /b 1:"') do set fltok3=%%a
echo(%fltok3%
Hackish =
#echo off
FOR %%A IN (.\xdrive\*.txt) DO (
FOR /F "usebackq tokens=3 delims=," %%m IN ("%%A") DO (
IF "%%m" == "F01" (xcopy /Y "%%A" .\Outbound)
GOTO:EOF
)
)
So all you're doing is escaping the loop after the first pass instead of continuing onto the next line.

Resources