Reputation: 3
I am using the below code and when it is run, the code in the REM 2 block does not work. If I change the order of REM 1 block and REM 2 block the first one works correctly.
Please advice if you faced a similar situation
Thanks, Roshan
@ECHO OFF
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
SET /A Count_tif=0
SET /A Count_lst=0
SET /A Count_cust_files = -1
FOR %%A IN (%Date%) DO (
FOR /F "tokens=1-3 delims=/-" %%B in ("%%~A") DO ( SET Today=%%D%%B%%C )
)
SET Today=%Today: =%
REM ********************** To generate .lst files for Customerx.txt ****************************
ECHO Generating .lst files
@FOR %%A IN (C:\Imaging\COMWC\SQR\Customer?.txt) DO SET /A Count_cust_files += 1
for /l %%x in (1, 1, %Count_cust_files%) do (
REM 1 block
FOR /f %%c IN (C:\Imaging\COMWC\SQR\Customer%%x.txt) DO (
FOR /f %%d IN ('dir /b /a-d \\albaix41\HPII_data\%Today%\WCBFMISWCIMGS%%x\%%c_*.tif') DO ECHO(%%d)>>\\albaix41\HPII_data\%Today%\WCBFMISWCIMGS%%x\%%c.lst
)
REM 2 block
FOR /f %%a IN (C:\Imaging\COMWC\SQR\Customer%%x.txt) DO (
FOR /f %%b IN ('dir /b /a-d \\albaix41\HPII_data\%Today%\WCBFMISWCIMGS%%x\%%a.tif') DO ECHO(%%b)>\\albaix41\HPII_data\%Today%\WCBFMISWCIMGS%%x\%%a.lst
)
)
Upvotes: 0
Views: 58
Reputation: 80213
I'd suggest, since you haven't said what you mean by 'doesn't "work"' that there may be two problems.
First one is that you have > in the #2 block, but >> in the #1 block. > will create the file anew on each iteration (you you'll get only the last iteration.) >> appends to any existing file or creates a new file if the file doesn't already exist, hence it will accumulate data.
The second problem is echo(whatever)
.
echo(
will produce a new line if it is provided with no data to echo
. echo
on its own will generate an echo status report
, that is, echo is ON
(or OFF)
Hence, echo(%%b)
does not do as you appear to expect. echo(%%b
will display %%b
-or a blank line if %%b
is undefined. Here's the critical point - the following ) then does not match the ( in the echo, but matches the ( in a FOR
.
Solution: replace echo(%%b)
with echo(%%b
. If you really want the contents of %%b
shown within round brackets, then use echo((%%b)
Upvotes: 1
Reputation: 41297
A problem is this line includes a trailing space and you are not using double quotes to handle spaces. I do see the following set statement, but it's better to realise the issue with this particular space.
DO ( SET Today=%%D%%B%%C )
:: use this instead:
DO (SET Today=%%D%%B%%C)
While this works with the set /a command it's better to use the following syntax - whitespace matters in some places in batch files and if you get used to putting spaces within variable names then it will bite you one day:
DO SET /A Count_cust_files += 1
:: use this instead:
DO SET /A Count_cust_files+=1
This is a better way to generate a timestamp for a file:
The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.
@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
Upvotes: 0