connect strings for python

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tobiasbfft16
Posts: 3
Joined: 05 Oct 2015 03:43

connect strings for python

#1 Post by tobiasbfft16 » 05 Oct 2015 04:21

This is my code:


@Echo off
echo ---------------------------------------------
echo ------ KLM file wird erstellt... ------
echo ---------------------------------------------
echo.
echo.
setlocal enabledelayedexpansion

For /R %%G IN (*.txt) DO set myvar=!myvar! %%G

python PositionTracer_v1.1.py %myvar%
pause



my question: As you see, i want to call a python script. So far it works, but when i want to call it with arguments (in my case the String "myvar", which i collected of the names of .txt files in the Folder i currently am) it does not work.
looking at the string myvar, i show's exactly what i want to see. So i am just searching a way to tell the commandline to execute the python script with the the string behind as its Argument.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: connect strings for python

#2 Post by Squashman » 05 Oct 2015 06:57

Your python program needs to be able to accept the command line arguments that you are trying to pass it. This is not a python forum. So if you have any questions regarding batch files please continue with this thread.

tobiasbfft16
Posts: 3
Joined: 05 Oct 2015 03:43

Re: connect strings for python

#3 Post by tobiasbfft16 » 06 Oct 2015 01:31

Please read carefull. The question i asked is not a python problem but a batch topic. The Python programm works well if i type in the command and the arguments (= the files) manually. Now i want the batch programm to do this for me. It should parse all the files in the folder and save the paths in a string. this string should be just added to the python command and sent to the commandline to execute it.

tobiasbfft16
Posts: 3
Joined: 05 Oct 2015 03:43

Re: connect strings for python

#4 Post by tobiasbfft16 » 06 Oct 2015 01:41

I found my mistake: myvar saves the whole path, but i just the filenames. can you show me the code needed to just save the filenames?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: connect strings for python

#5 Post by Squashman » 06 Oct 2015 07:18

tobiasbfft16 wrote:I found my mistake: myvar saves the whole path, but i just the filenames. can you show me the code needed to just save the filenames?

myvar is not what is giving you the whole path. The FOR /R command is. The FOR /R command help file specifically says:

Code: Select all

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.


So that begs the question. Are all the files you really want to process in sub folders? If they are not then just use a normal FOR command.

IF you really need to use the FOR /R and just want the file name versus the whole path then use the command modifiers. This again is also explained in the help file for the FOR command.

Code: Select all

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.


Examples:

Code: Select all

H:\move\archive>for %G in (*.txt) do @echo %G
file1.txt
file2.txt

H:\move\archive>for /R %G in (*.txt) do @echo %G
H:\move\archive\file1.txt
H:\move\archive\file2.txt
H:\move\archive\old\file3.txt
H:\move\archive\old\file4.txt

H:\move\archive>for /R %G in (*.txt) do @echo %~nxG
file1.txt
file2.txt
file3.txt
file4.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: connect strings for python

#6 Post by foxidrive » 08 Oct 2015 04:37

tobiasbfft16 wrote:Please read carefull. The question i asked is not a python problem but a batch topic.


It is certainly a python issue if the python script doesn't support multiple files and pathnames and we had no information about the function of the python script itself to judge if what you were attempting to do was possible.

We also have no detail on what filenames and paths it is meant to using - because short pathnames != long pathnames.

Squashman told you the same thing viewtopic.php?p=43192#p43192

Post Reply