Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
somu_june
- Posts: 30
- Joined: 19 Jun 2013 20:26
#1
Post
by somu_june » 26 Jun 2013 10:32
Hi,
I have a batch script, in the batch script one of the condition is to check number of files in a directory and if the count is greater than 0 then perform some functions and not greater than zero then don't perform any function. I executed below script but this is giving an message saying 0 was unexpected at this time. and not going to the correct label "No". See below script
Code: Select all
echo off
REM =====Below %~f0 will provide obsolute path with filename
for /F %%i in ("%~f0") do set filename=%%~ni
REM ====== Below command will provide the current date with format YYYYMMDD_HH_MM_SS
SET CurrentDate=%date:~-4,4%%date:~-10,2%%date:~-7,2%
SET hh=%time:~-11,2%
if %hh%==0 set hh=00
if %hh%==1 set hh=01
if %hh%==2 set hh=02
if %hh%==3 set hh=03
if %hh%==4 set hh=04
if %hh%==5 set hh=05
if %hh%==6 set hh=06
if %hh%==7 set hh=07
if %hh%==8 set hh=08
if %hh%==9 set hh=09
SET mm=%time:~-8,2%
SET ss=%time:~-5,2%
REM ===Below command will echo file name with batch script name appended with timestamp
echo %filename%_%CurrentDate%_%hh%_%mm%_%ss%.log
REM ===Below commmand willl provide the directory name from where the script is triggered
for %%F in ("%~f0") do set dirname=%%~dpF
SET basedirname=%dirname%
SET resultdir=Result\
SET Currentdir=Current\
ECHO %basedirname%%resultdir%%filename%_%CurrentDate%_%hh%_%mm%_%ss%.log
:: Below cnt will provide the number of files in the Current directory
set count=0
cd %basedirname%%Currentdir%
set cnt=0
for %%A in (*.dsx) do set /a cnt+=1
echo Filecount=%cnt%
cd %basedirname%
Set IntCount=0
IF %Filecount% GTR %IntCount% Goto No
ECHO Great! You don't have any files to process.
PAUSE
GOTO :EOF
:No
ECHO Great! You have files to process.
PAUSE
GOTO :EOF
Thanks,
Raju
EDITED BY MOD TO INCLUDE CODE TAGS
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#2
Post
by Squashman » 26 Jun 2013 10:42
Hi Raju, if you could in the future please enclose your code in CODE tags. I added them to your post.
Thanks
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#3
Post
by Squashman » 26 Jun 2013 11:05
Does this give you a clue.
Code: Select all
for %%A in (*.dsx) do set /a cnt+=1
echo Filecount=%cnt%
Set IntCount=0
IF %Filecount% GTR %IntCount% Goto No
Where do you ever set the variable FileCount to a number?
At this point the variable FileCount is undefined!
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#4
Post
by Squashman » 26 Jun 2013 11:16
You also have some redundant and unnecessary code. There is no need to use these two for Loops and this SET statement.
Code: Select all
for /F %%i in ("%~f0") do set filename=%%~ni
for %%F in ("%~f0") do set dirname=%%~dpF
SET basedirname=%dirname%
If you want the base file name of the batch file without the extension then just use
%~n0If you want the full path to the batch file without the file name then just use
%~dp0Why bother setting the variable dirname and then assigning it to basedirname. Unless you are going to further manipulate the basedirname variable there is no need to do that assignment.
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#5
Post
by Squashman » 26 Jun 2013 11:21
And Again this is all really extra code that is not needed.
Code: Select all
for %%A in (*.dsx) do set /a cnt+=1
echo Filecount=%cnt%
Set IntCount=0
IF %Filecount% GTR %IntCount% Goto No
You can really strip it down to this.
Code: Select all
for %%A in (*.dsx) do set /a cnt+=1
IF %cnt% GTR 0 Goto No
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#6
Post
by Squashman » 26 Jun 2013 11:28
You could in theory just change your code to this.
Then you could remove the for loop to count the number of DSX files.
-
somu_june
- Posts: 30
- Joined: 19 Jun 2013 20:26
#7
Post
by somu_june » 26 Jun 2013 11:28
Thanks for catching the bug. It got resolved
Raju