I have 2 directories: dirA and dirB. We will only take dirB at the moment.
dirB contains many files, named of course with different names/extension.
I need to extract the first 6 letters from each of them, after this I can continue with the 2nd part of the program, for now I only need to extract the first 6 letters from each file and write them on a txt files.
Here’s the code I wrote, the FOR is the only one that doesn’t work well, or better, instead of having the same variable: STR named file1 at loop1, file2 at loop2 etc.
I have STR = file1file2fileN.
var is file1f (6 letters already).
@ECHO off
CLS
SET dirA=C:\dirA
SET dirB=C:\dirB
SET lista=C:\lista.txt
IF NOT EXIST %dirB% GOTO DNF
IF NOT EXIST %dirA% GOTO DNF
setLocal EnableDelayedExpansion
CD %dirB%
DIR /B * > %lista%
:MAIN
CLS
SET /P S=Last Filename: || Set S=null
:LOOP
IF /I "%S%"=="null" GOTO MAIN
FOR /F "tokens=*" %%I IN (%lista%) DO SET str=!str!%%I
SET var=!str:~0,6!
echo !str! > str.txt
echo !var! >> file.txt
SET !str!%%I
IF /I %S%==!var! GOTO:EOF
IF /I %S% NEQ !var! GOTO SNF
GOTO LOOP
:DNF
ECHO.
ECHO %dirA% - %dirB% not found
GOTO:EOF
:SNF
ECHO.
ECHO %S% Not found
TIMEOUT /T 2
GOTO MAIN
GOTO:EOF
endLocal
end
Excuse me. I think I don’t understand what your code tries to achieve. However, this Batch segment “extract the first 6 letters from each file and write them on a txt files”:
It is more efficient to enclose the whole FOR command in parentheses and use a single > redirect operation, than use an >> append operation for each file name.