Need Batch File Help

L

lonepalm24

Guest
First off, sorry if this is not posted in the right forum. I wasn't sure if it was more appropriate here or in the "Chillout Room" or elsewhere.

I'm a noob when it comes to batch scripts, so I'd appreciate any help I could get. I tried doing all sorts of searches to find my answer (at least one I could understand) and was not able to figure it out. I have a batch file that loops through a directory and executes anything with the .py extension. I want to relocate the batch file to a subfolder, so I need to tell the script to move up a directory. Currently, the file is:

Code:
for /f %%a IN ('dir /b "*.py"') do python %%a
pause

What do I need to do in order to be able to relocate the script to a subfolder and have it go up a directory to execute the loop? Currently, the script is in the same directory as the .py files and it works great, but I want to relocate the .bat to a subdirectory. Ideally, the solution would be relative and not absolute.

Thanks!
 

My Computer

I'm not in a position to test so if you try this then do it on dummy data:

Code:
for /f %%a IN ('dir .. /b "*.py"') do python %%a
pause

This should then run the script against the folder 1 level back

i.e. c:\users\jamie\test to c:\users\jamie


Forgive me if i have misunderstood or that's not the correct solution.

Regards,
Jamie
 

My Computer

System One

  • OS
    Windows 8.1 Pro x64
    Computer type
    PC/Desktop
    System Manufacturer/Model
    Self Built
    CPU
    Intel Core i5-2500K @ 3.30GHz - S1155
    Motherboard
    Asus P8P67 LE Rev3, Intel P67, S115
    Memory
    8GB Corsair DDR3 XMS3, PC3-12800
    Graphics Card(s)
    NVIDIA GeForce GTX 650
    Sound Card
    On-Board
    Monitor(s) Displays
    3 x 24" {Extended Display}
    Screen Resolution
    1920 x 1080
    Hard Drives
    300GB Seagate Barracuda 7200
    PSU
    550W Coolermaster GX550
    Case
    Silverstone Precision PS04B
    Cooling
    Stock
    Keyboard
    Logitech K120
    Mouse
    World of Warcraft Cataclysm MMO Gaming Mouse
    Internet Speed
    80MB
    Browser
    Chrome
    Antivirus
    MSE / Windows Defender
I'm not in a position to test so if you try this then do it on dummy data:

Code:
for /f %%a IN ('dir .. /b "*.py"') do python %%a
pause

This should then run the script against the folder 1 level back

i.e. c:\users\jamie\test to c:\users\jamie


Forgive me if i have misunderstood or that's not the correct solution.

Regards,
Jamie

Thank you. Unfortunately, it does not work. I had output the results to a text file to see why it didn't and I see a lot of "No such file or directory" errors. Looks like it was entering the "python Volume" , "python Directory" , or even "python [then a date]" for a command line argument instead of "python FILENAME.py" .

Any other suggestions?
 

My Computer

My Computer

System One

  • OS
    PC-DOS v1.0
    Computer type
    PC/Desktop
    System Manufacturer/Model
    IBM
    CPU
    Intel 8088, 4.77MHz
    Memory
    16K, 640K max
    Graphics Card(s)
    What's that?
    Sound Card
    Not quite
    Screen Resolution
    80 X 24 text
    Hard Drives
    dual 160KB 5.25-inch disk drives
How about another test...

Again, test this on dummy data, i'm writing this blind as i'm at work and can't really test it.
Sorry if this doesn't help either :D

First create a list of files you want to run python against with the full path included...
Code:
dir *.py /s /b "directory here" >c:\users\%username%\desktop\files.txt

Now run the for loop against that text file
Code:
for /f "tokens=*" %%a in (c:\users\%username%\desktop\files.txt) do (python "%%a")
pause

-Jamie
 

My Computer

System One

  • OS
    Windows 8.1 Pro x64
    Computer type
    PC/Desktop
    System Manufacturer/Model
    Self Built
    CPU
    Intel Core i5-2500K @ 3.30GHz - S1155
    Motherboard
    Asus P8P67 LE Rev3, Intel P67, S115
    Memory
    8GB Corsair DDR3 XMS3, PC3-12800
    Graphics Card(s)
    NVIDIA GeForce GTX 650
    Sound Card
    On-Board
    Monitor(s) Displays
    3 x 24" {Extended Display}
    Screen Resolution
    1920 x 1080
    Hard Drives
    300GB Seagate Barracuda 7200
    PSU
    550W Coolermaster GX550
    Case
    Silverstone Precision PS04B
    Cooling
    Stock
    Keyboard
    Logitech K120
    Mouse
    World of Warcraft Cataclysm MMO Gaming Mouse
    Internet Speed
    80MB
    Browser
    Chrome
    Antivirus
    MSE / Windows Defender
I'm not sure one can do this dynamically - but see if this helps...

Batch: Get the full path of the folder in the previous directory? - Stack Overflow

(Unfortunately cmd is not my scripting language of choice)

Thank you for providing that. Unfortunately, my level of comprehension/coding is very low for this stuff and trying to translate what the page said into my loop is just not something I am able to do. I don't typically like asking people to do the work for me, but in this case, I am just completely lost. Thanks for your help/suggestion!
 

My Computer

How about another test...

Again, test this on dummy data, i'm writing this blind as i'm at work and can't really test it.
Sorry if this doesn't help either :D

First create a list of files you want to run python against with the full path included...
Code:
dir *.py /s /b "directory here" >c:\users\%username%\desktop\files.txt

Now run the for loop against that text file
Code:
for /f "tokens=*" %%a in (c:\users\%username%\desktop\files.txt) do (python "%%a")
pause

-Jamie

I'll give this a shot, but my ideal solution wouldn't require specifying an absolute path to anything as I'm not sure if the directory will change in the future. Ideally everything would be relative, but I do understand there are limitations.
 

My Computer

I used to do batch filing YEARS ago, from DOS 3.3 to DOS 6.22; Window 3.1 to WFW 3.11. [My autoexec.bat w/ if exist, if not exit, choice, etc. once ran 4K bytes; my lan.bat ran 2-3K]. Hard-coding, exactly specifying the directory to be acted upon is probably the safest thing to do. If the acted upon directory changes from X to Y, yes, you will have to "reCode" your existing batchfile.
 

My Computer

System One

  • OS
    Windows 7 Pro 64bit [MS blue-disk set]
    Computer type
    PC/Desktop
    System Manufacturer/Model
    2 Acers & 1 Antec[?]
    CPU
    i7 in 2 Acers, i5 in desktop
    Motherboard
    Desktop w/Gigabyte
    Memory
    Two w/16GB, 1 w/8GB
    Graphics Card(s)
    Laptops GameWorthy; Desktop maybe GameWorthy
    Monitor(s) Displays
    flatscreens; 2 are BluRay worthy
    Screen Resolution
    1368x768; 1600x900
    Hard Drives
    1TB internals; 2 ext usb WD 1TB HDs
    PSU
    what's PSU?
    Cooling
    Regular plus external fans
    Keyboard
    desktio w/PS2
    Mouse
    desktop w/PS2
    Internet Speed
    DSL middle level [160?]
    Browser
    from Netscape 0.9 to FF 36
    Antivirus
    well-balanced, well-configured mult-layered defense is best
    Other Info
    From MS-DOS 3.3, MS-DOS 6.22, from Windows 3.1 to WFW 3.11 to Windows 95-98SE, now to Windows 7 Pro.
    Security for now: Windows 7 Firewall, Emsisoft AM, MSE [scan-only], SpywareBlaster, Ruiware/BillP combine
I used to do batch filing YEARS ago, from DOS 3.3 to DOS 6.22; Window 3.1 to WFW 3.11. [My autoexec.bat w/ if exist, if not exit, choice, etc. once ran 4K bytes; my lan.bat ran 2-3K]. Hard-coding, exactly specifying the directory to be acted upon is probably the safest thing to do. If the acted upon directory changes from X to Y, yes, you will have to "reCode" your existing batchfile.

Thanks for your input as well! Perhaps the better route might be trying to figure out how to get python to do this work for me instead of trying to get a .bat file to do what I need? Or even easier, just leave the .bat file in the current directory and keep using my code that I know works...

Thanks!
 

My Computer

From 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.

If Command Extensions are enabled, the following additional
forms of the FOR command are supported:

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

If set contains wildcards, then specifies to match against directory
names instead of file names.

FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

Walks the directory tree rooted at [drive:]path, executing the FOR
statement in each directory of the tree. If no directory
specification is specified after /R then the current directory is
assumed. If set is just a single period (.) character then it
will just enumerate the directory tree.

FOR /L %variable IN (start,step,end) DO command [command-parameters]

The set is a sequence of numbers from start to end, by step amount.
So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
generate the sequence (5 4 3 2 1)

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

or, if usebackq option present:

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ('string') DO command [command-parameters]
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]

file-set is one or more file names. Each file is opened, read
and processed before going on to the next file in file-set.
Processing consists of reading in the file, breaking it up into
individual lines of text and then parsing each line into zero or
more tokens. The body of the for loop is then called with the
variable value(s) set to the found token string(s). By default, /F
passes the first blank separated token from each line of each file.
Blank lines are skipped. You can override the default parsing
behavior by specifying the optional "options" parameter. This
is a quoted string which contains one or more keywords to specify
different parsing options. The keywords are:

eol=c - specifies an end of line comment character
(just one)
skip=n - specifies the number of lines to skip at the
beginning of the file.
delims=xxx - specifies a delimiter set. This replaces the
default delimiter set of space and tab.
tokens=x,y,m-n - specifies which tokens from each line are to
be passed to the for body for each iteration.
This will cause additional variable names to
be allocated. The m-n form is a range,
specifying the mth through the nth tokens. If
the last character in the tokens= string is an
asterisk, then an additional variable is
allocated and receives the remaining text on
the line after the last token parsed.
usebackq - specifies that the new semantics are in force,
where a back quoted string is executed as a
command and a single quoted string is a
literal string command and allows the use of
double quotes to quote file names in
file-set.

Some examples might help:

FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

would parse each line in myfile.txt, ignoring lines that begin with
a semicolon, passing the 2nd and 3rd token from each line to the for
body, with tokens delimited by commas and/or spaces. Notice the for
body statements reference %i to get the 2nd token, %j to get the
3rd token, and %k to get all remaining tokens after the 3rd. For
file names that contain spaces, you need to quote the filenames with
double quotes. In order to use double quotes in this manner, you also
need to use the usebackq option, otherwise the double quotes will be
interpreted as defining a literal string to parse.

%i is explicitly declared in the for statement and the %j and %k
are implicitly declared via the tokens= option. You can specify up
to 26 tokens via the tokens= line, provided it does not cause an
attempt to declare a variable higher than the letter 'z' or 'Z'.
Remember, FOR variables are single-letter, case sensitive, global,
and you can't have more than 52 total active at any one time.

You can also use the FOR /F parsing logic on an immediate string, by
making the file-set between the parenthesis a quoted string,
using single quote characters. It will be treated as a single line
of input from a file and parsed.

Finally, you can use the FOR /F command to parse the output of a
command. You do this by making the file-set between the
parenthesis a back quoted string. It will be treated as a command
line, which is passed to a child CMD.EXE and the output is captured
into memory and parsed as if it was a file. So the following
example:

FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i

would enumerate the environment variable names in the current
environment.

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string

The modifiers can be combined to get compound results:

%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid
values. The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.
 

My Computer

System One

  • OS
    Windows 8.1
    Computer type
    Laptop
    System Manufacturer/Model
    Toshiba Satellite C55t Series
    CPU
    Intel
    Browser
    IE & Firefox
can you post a link to that webpage - thank you

i'm looking to search FOR driveletter/directory/ subdirectory/filename
so as to display the whole path to filename
 

My Computer

System One

  • OS
    Windows 3.1 > Windows 10
    Computer type
    PC/Desktop
    System Manufacturer/Model
    Dell XPS 8700
    CPU
    I7
    Memory
    24 GB
Back
Top