simple BAT file not behaving as expected

Hi,
I am running the following bat file on windows 7
@echo off 
Rem This is for listing  all the files in the directory 
dir "P:\My Documents" > P:\My Documents\lists.txt 
echo "The program has completed"

Open in new window

I am expecting  a file "lists.txt" to be created listing the contents of the directory but this is not happening....
Is there someway I can log what is going on  to find out what is happening?
thanks
blossomparkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JohnBusiness Consultant (Owner)Commented:
Open cmd.exe and run dir "P:\my documents" and what do you see?

Do you have non-admin permission to the folder?
oBdACommented:
The path to the target file needs to be enclosed in double quotes as well:
@echo off 
Rem This is for listing  all the files in the directory 
dir "P:\My Documents" >"P:\My Documents\lists.txt"
echo "The program has completed"

Open in new window


P.S.: You'll find your listing in the file "P:\My" (no extension).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
Is there someway I can log what is going on  to find out what is happening?

There isn't a lot of debug capability for BAT scripts, but one thing you can do is turn ECHO ON for the bat script, and then run it from a CMD prompt so you can see the output.  For small scripts the output is manageable, for large ones it can be hard to sort through.  By having ECHO ON the command processor will echo the command that it is going to execute before it executes it.  This can often be helpful in following logic flow, and also seeing variables resolved to their values.

To get a sense for what that looks like just change the ECHO OFF to ECHO ON at the top of your script (or remove that line, since typically ECHO ON is the default).


»bp
Bill PrewIT / Software Engineering ConsultantCommented:
One other small tip is to use variables for constants when it makes sense.  And when doing that for file and path names, always leave the double quotes off when defining the variable, and then add them whenever you reference the variable.  Add the double quotes to all references even if you don't need them with current paths / names.  At some point in the future the path or filename might contain a blank and break the script without them...

Using meaningful names for file values makes the script easier to understand and read, and avoids repeating the same constants multiple times if needed.

So you could do:

@echo off 
setlocal

set BaseDir=P:\My Documents
set ListFile=P:\My Documents\lists.txt

Rem This is for listing  all the files in the directory 
dir "%BaseDir%" > "%ListFile%"

echo "The program has completed"

Open in new window


»bp
blossomparkAuthor Commented:
Thanks John, I could see the contents of the directory, regarding  "non-admin permissions to the folder", not sure what my permissions are....

oBdA...that fixed it,thank you,  the double quotes
regarding
"P.S.: You'll find your listing in the file "P:\My" (no extension). "
did not see this file....

Bill, that's very useful thank you

Big thank you to you all :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
batfile

From novice to tech pro — start learning today.