Im trying to build a batch file.
I have 30 csv files in a folder
My intention is
EG: lookup1.bat
@echo 107=B_05-
@echo 097=B_06-
@echo 149=B_07-
@echo 109=B_08-
@echo 101=B_09-
@echo 105=B_10-
@echo 098=B_11-
So here i will get "B_06-"
Here is my code , i have only basic ideas about looping and im struggling a lot.Thanks for any help.
@echo on
setlocal enabledelayedexpansion
for %%a in ("*.csv") do (
set FileName=%%~na
goto:stepa
goto:eof
)
:stepa
for /f "tokens=1 delims=_" %%a in ("%FileName%") do (
set A=%%a
echo %A%
)
@SET MN=%A%
@FOR /F "tokens=1,2 delims==" %%i IN ('lookup1.bat') DO @IF %%i EQU %MN% SET MW=%%j
@ECHO The board number corresponding to %MN% is %MW%.
set "str=%MW%%FileName%"
echo "%str%"
Ren "!FileName!" "!str!"
:eof
You have a series of problems with the structure of your program. If you want to call a subroutine from inside a for
command the right way is using call
, not goto
, and the goto :eof
command must be placed after the for
ends. However, this code is simple enough so it don't requires a subroutine.
The table lookup method is more efficient (and also simpler, IMHO) if you use an array to load the table just once, and then directly access its elements via an index .
@echo off
setlocal EnableDelayedExpansion
rem Create the lookup file with lookup1.bat file,
rem this program create lines like this one:
rem 097=B_06-
call lookup1.bat > lookup1.txt
rem Load the lookup table from the file,
rem this method create _array elements_ like this one:
rem set "table[097]=B_06-"
for /F "tokens=1,2 delims==" %%a in (lookup1.txt) do set "table[%%a]=%%b"
rem Process the files in current folder,
rem file name format is this:
rem ###_restOfFileName.csv
for /F "tokens=1* delims=_" %%a in ('dir /A:-D /B *.csv') do (
@ECHO The board number corresponding to %%a is !table[%%a]!.
ren "%%a_%%b" "!table[%%a]!%%b"
)
You may test if the board number is not defined for a file via an additional if not defined table[%%a] ...
command placed inside the for
.
You may directly create the lookup1.txt
file with your text editor; just eliminate all these @echo
parts from lookup1.bat
and change the extension.
You may review a detailed explanation on array use in Batch files at this answer .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.