windows batch programming, for command

March 28, 2010 at 09:53:26
Specs: Windows xp
Dear all, I need your help please

I have a batch file on windows.
I need to read from a text file a list of file names, the content of the text file is for example filename1;filename2;filename3


Then, i need to verify in a directory that is an input parameter in the script if the physical files which names are listed in the text file (filename1,filename2...)
exist in this directory; and write output to a text file a message indicating if each file is found or not.

The code i wrote but did not return the desired result is:

[CODE]

@ECHO OFF
echo %date%,%time% Generate list of available and unavailable files >.\log.txt

:start_of_file
SET /P src_dir="Enter the absolute path for Source Directory where physical Files are present (including the final \ ): [e.g: .\DATA\RD\]:"
SET /P src_file="Enter the name of file including the list of the filenames (including the extension e.g: .txt):"

ECHO The list of filenames are in %src_file%>>.\log.txt
ECHO The physical files are in %src_dir%>>.\log.txt
set COUNTER=0

FOR /F "delims=;" %%A IN (%src_file%) DO(
set /a COUNTER+=1
set FILENAME!COUNTER!=%%A
goto TEST_FILE_EXISTENCE
:TEST
)
GOTO END

:TEST_FILE_EXISTENCE
IF EXIST %src_dir%%FILENAME% (
ECHO The file you are looking for %FILENAME% has been found in %src_dir%>>.\log.txt
GOTO TEST
) ELSE (
ECHO The file you are looking for %FILENAME% has not been found in %src_dir%>>.\log.txt
GOTO TEST
)

:END


[/CODE]


and the log displays the following:

Sat 03/27/2010,12:51:19.95 Generate list of available and unavailable files
The list of filenames are in FILES_LIST.txt
The physical files are in D:\Scripts\Data\10102010\



See More: windows batch programming, for command

Report •

#1
March 28, 2010 at 15:57:59
if you use goto in a for-loop, i think it kills the loop, which is unfortunate. you should use "call" instead of goto:
call :test_file
(be sure to put the colon in front of the subroutine-name)
take the counter off the filename, for your purposes its not needed:
set FILENAME!COUNTER!=%%A
to:
set FILENAME=%%a
and lastly, remove that :test label! now you don't need it and my experience a label imm. preceding the closing parenth disrupts the for-loop. The test_file routine will automatically
return to the main routine:
:test_file
if exist x (
echo yes
) else (
echo no
)
:--- here is end of the subroutine, it goes back to main routine

Report •

#2
March 29, 2010 at 00:35:14
thanks for ur response, i tried ur suggestion

[CODE]

@ECHO OFF
setlocal enabledelayedexpansion
echo %date%,%time% Generate list of available and unavailable files >.\log.txt

:start_of_file
SET /P src_dir="Enter the absolute path for Source Directory where Files are present (including the final \ ): [e.g: .\DATA\RD\]:"
SET /P src_file="Enter the name of file including the list of the names of files (including the extension e.g: .txt):"

ECHO The list of files are in %src_file%>>.\log.txt
ECHO The physical files are in %src_dir%>>.\log.txt


FOR /F "delims=;" %%A IN (%src_file%) DO(
set FILENAME=%%A
set x=%src_dir%%FILENAME%
echo x>>.\log.txt
call :TEST_FILE_EXISTENCE
)
GOTO END
:TEST_FILE_EXISTENCE
IF EXIST x (
ECHO The file you are looking for %FILENAME% has been found in %src_dir%>>.\log.txt
) ELSE (
ECHO The file you are looking for %FILENAME% has not been found in %src_dir%>>.\log.txt
)
:END


[/CODE]

and the result in the log was the same:

Mon 03/29/2010,10:15:04.82 Generate list of available and unavailable files
The list of files are in FILES_LIST.txt
The physical files are in D:\Scripts\Data\10102010\

It seems that it does not get into the for loop


please need ur help



Report •

#3
March 29, 2010 at 02:09:20
sorry, my boob:
IF EXIST x (
ECHO The file you are looking for %FILENAME% has been found in %src_dir%>>.\log.txt
::
should be:
if exist %x% (
::------
my apologies... but paybacks were hell! i def. spent some time trying to find this gremlin!! waiting for the next failure...

Report •

Related Solutions

#4
March 29, 2010 at 03:55:00
unfortunately, this returns the same result, thank u for ur followup, need ur help


[CODE]

@ECHO OFF
setlocal enabledelayedexpansion
echo %date%,%time% Generate list of available and unavailable files >.\log.txt

:start_of_file
SET /P src_dir="Enter the absolute path for Source Directory where Files are present (including the final \ ): [e.g: .\DATA\RD\]:"
SET /P src_file="Enter the name of file including the list of the names of files (including the extension e.g: .txt):"

ECHO The list of files are in %src_file%>>.\log.txt
ECHO The physical files are in %src_dir%>>.\log.txt


FOR /F "delims=;" %%A IN (%src_file%) DO(
set FILENAME=%%A
set x=%src_dir%%FILENAME%
echo x>>.\log.txt
call :TEST_FILE_EXISTENCE
)
GOTO END
:TEST_FILE_EXISTENCE
IF EXIST %x% (
ECHO The file you are looking for %FILENAME% has been found in %src_dir%>>.\log.txt
) ELSE (
ECHO The file you are looking for %FILENAME% has not been found in %src_dir%>>.\log.txt
)
:END


[/CODE]


Report •

#5
March 29, 2010 at 06:04:23
Oh, why are Monday mornings so boring?

Assuming it's one entry per line, this works:

@ECHO OFF
SET /P src_dir="Enter the absolute path for Source Directory where Files are present: [e.g: C:\DATA\RD\]:"
SET /P src_file="Enter the name of file including the list of the names of files (including the extension e.g: .txt):"
SET src_dir=%src_dir:"=%
SET src_file=%src_file:"=%

>log.txt (ECHO %date%,%time% Generate list of available and unavailable files
ECHO The list of files are in %src_file%
ECHO The physical files are in %src_dir%
FOR /F "usebackq delims=" %%A IN ("%src_file%") DO IF EXIST "%src_dir%\%%A" (ECHO %%A exists) ELSE ECHO %%A doesn't exist)

Assuming it's a list of files between ';', this works (but is much slower):
@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET /P src_dir="Enter the absolute path for Source Directory where Files are present: [e.g: C:\DATA\RD\]:"
SET /P src_file="Enter the name of file including the list of the names of files (including the extension e.g: .txt):"
SET src_dir=%src_dir:"=%
SET src_file=%src_file:"=%

>log.txt (ECHO %date%,%time% Generate list of available and unavailable files
ECHO The list of files are in %src_file%
ECHO The physical files are in %src_dir%)

FOR /F "usebackq delims=" %%A IN ("%src_file%") DO (
  SET A=%%A
  SET A="!A:;=";"!"
  SET A=!A:""="!
  CALL :ReadLine !A!
)
GOTO :EoF

:ReadLine
IF "%~1"=="" GOTO :EoF
>>log.txt (IF EXIST "%src_dir%\%~1" (ECHO %~1 exists) ELSE ECHO %~1 doesn't exist)
SHIFT
GOTO ReadLine


Report •

#6
March 30, 2010 at 10:58:35
thank u very much, you are my best

Report •

Ask Question