1.

Solve : Command won't work from notepad .bat file?

Answer»

Hello, first post.

I have a very intriguing problem and I'm hoping that it's not just a very very stupid mistake.

I opened notepad typed in my commands and then clicked Save As. I changed the "Save As Type" to All files and then saved the file as mybat.bat

Here is the CODE I have in my newly created .bat file:
D:
cd Video
FOR /F "tokens=1* skip=1 delims=\" %i IN (txt_files.txt) DO @echo %i %j >> txt1_files.txt
pause

lines 1 and 2 navigate to the correct directory
line 3 reads a .txt file, grabs a delimited string and puts it into the new .txt file

When I run mybat.bat file the window comes up for a fraction of a second and disappears. The new txt1_files.txt file is never created and the "pause" statement is never reached. When I open a cmd window and type the commands in (in the same order and typed exactly the same) it works, flawlessly.

So, the question is, am I doing something horribly/stupidly wrong, or can the FOR statement not be used in .bat files created from notepad, or am I missing something else all together.

I think I've included all the information needed, if I haven't I apologize, I've been struggling with this for quite some time.

Thanks in advance!Quote

When I run mybat.bat file the window comes up for a fraction of a second and disappears.

This suggests to me that you are running it from Windows Explorer by double clicking. When trouble shooting a batch file that behaves in this way, a good idea is to run it by OPENING a command prompt, changing the current drive and directory as appropriate, and finally typing the batch file name at the prompt. That way if the batch crashes out you just get the prompt back and you can see any error messages. These may give you (and us) an idea of what is happening.

In this case, it won't be necessary, as the problem is obvious. Where you use one percent sign in a FOR variable at the prompt, you use TWO of them in a batch file. Thus %i and %j NEED to become %%i and %%j.


Quote
I've been struggling with this for quite some time.

As with most commands, help is available by typing the command at the prompt with the /? switch.

Code: [Select]C:\>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.
(That is just the beginning of the very detailed FOR help, which runs to about 4 screens)

Perhaps you should get used to using the prompt more.

Thank you so MUCH. This fixed the problem. I was beating myself up over this error. Is there anywhere you could point me towards to read up on the little intricacies like this so I can avoid the ANGUISH and having to post a seemingly obvious problem here?

Thanks again!Quote from: jinky1087 on May 22, 2009, 11:46:24 PM
Is there anywhere you could point me towards to read up on the little intricacies like this

Quote from: Me, above
As with most commands, help is available by typing the command at the prompt with the /? switch.

Or there are plenty of command prompt help sites you can find by using Google.

This is one I have bookmarked

http://www.ss64.com/nt/





Here's a tip. If you use the CD command with the /D switch, you can change drive and directory at the same time. That is

Code: [Select]D:
cd Video
May be replaced with

Code: [Select]cd /d D:\Video


Discussion

No Comment Found