Creating batch file for filenames

Discussion in 'Windows Operating Systems' started by spotted, Jan 19, 2021.

  1. spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    414
    Location:
    Sydney
    So I'm getting a list of files using

    dir *.txt /s /b > list.txt

    Now say I want to actually format list.txt so that each line says

    python script.py "\1"

    where \1 is a filename, ie a line of list.txt

    Is there a way to do this?

    Currently I'm doing a regex search/replace using an editor
    It's clunky.
     
  2. NSanity

    NSanity Member

    Joined:
    Mar 11, 2002
    Messages:
    18,284
    Location:
    Canberra
    Code:
    $files = (Get-ChildItem -filter "*.txt" -Recurse | ForEach-Object { $_.FullName })
    foreach ($file in $files) {$myargs = 'script.py "'+$file+'"'
    invoke-command -script {.\python.exe $myargs}
    }
     
    Last edited: Jan 19, 2021
  3. OP
    OP
    spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    414
    Location:
    Sydney
    sorry I was unclear.
    I don't want to actually run it right now.
    I want to write an output list.bat

    then I would copy lines from the file and run in a cmd one at a time
     
  4. metamorphosis

    metamorphosis Member

    Joined:
    Feb 25, 2002
    Messages:
    2,066
    If your intent is to run a script over a set of files via msdos batch script I suggest the following:
    FORFILES /S /M *.txt /C "cmd /c call python script.py @file"
     
  5. OP
    OP
    spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    414
    Location:
    Sydney
    think of it as
    I know I"ll need to run this script over ALL the files in these subfolders
    but I don't know when or in what order

    So I'm making a bat file which I can either copy lines out to run
    or cut bits out and run as a bat file
     
  6. OP
    OP
    spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    414
    Location:
    Sydney
    invoke-command -script {.\python.exe $myargs}
    is having an issue

    python.exe: can't open file

    almost like it doesn't care that we've quoted the filenames?
     
  7. philquad

    philquad Member

    Joined:
    Jan 22, 2004
    Messages:
    1,571
    Location:
    nelson bay
    script kiddies, just use this https://www.den4b.com/products/renamer

    "jkin on the kiddies"
     
  8. OP
    OP
    spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    414
    Location:
    Sydney
    I would like to use this for another usage, but both the scrip and file are in paths with spaces in them. Usually I would cd to the script path, and call the full filename. But I can't do that with this. I can't work out how to put quotes in quotes
     
  9. Rass

    Rass Member

    Joined:
    Jun 27, 2001
    Messages:
    3,121
    Location:
    Brizbekistan

    Did you want this all done in one operation, or a series of operations?

    if as a series of discrete operations, you could create another batch file which does something like:

    FOR /F %%f In (list.txt) DO echo "python script.py %%f" >> newbatch.bat

    bear in mind, the tokenisation of the lines from the list.txt file mean that if there's any spaces in the filenames, the above command will not work well - you need to investigate that.
     
  10. PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,471
    Code:
    $listfile = "C:\crap\list.txt"
    $newfile = "C:\crap\newlist.txt"
    $PythonCmd = "python script.py "
    $OriginalList = get-content $listfile
    foreach ($entry in $OriginalList) {
    $pythonCmd + $entry | out-file -FilePath $newfile -Append
    }
    
    Edit: Just read a bit more.
    Dealing with escaping quotes and dealing with spaces using for in batch is balls. Just use powershell.

    Code:
    $pythonCmd + """$entry""" | out-file -FilePath $newfile -Append
     
    Last edited: Jan 20, 2021
    NSanity likes this.
  11. OP
    OP
    spotted

    spotted Member

    Joined:
    Nov 10, 2010
    Messages:
    414
    Location:
    Sydney
    Thanks I think I got PS working with Invoke-Expression
     
  12. Myne_h

    Myne_h Member

    Joined:
    Feb 27, 2002
    Messages:
    11,322
    You'd probably want to mod it slightly.

    for /f "tokens=*" %%f in ('dir *.txt /s /b') do echo python script.py %%f >> list.txt

    OP, if it's not working, try that one liner. Replace %% with % if you're just running it once off from the cmd line.

    If you need the filename in quotes change %%f to "%%f"

    Bear in mind this will APPEND to an existing list.txt
    If you want to recreate a new one every time, add echo. >list.txt in front of that line in the bat.
     
    Last edited: Jan 21, 2021
  13. metamorphosis

    metamorphosis Member

    Joined:
    Feb 25, 2002
    Messages:
    2,066
    Usually you can just use the alternative quote marks in this case - ie single-quote marks ' for the filename, within the double-quotes " block.
    Been a while since I checked this though
     
  14. Rass

    Rass Member

    Joined:
    Jun 27, 2001
    Messages:
    3,121
    Location:
    Brizbekistan
    Hmm... yes, that would work. I went down the path of removing the default delimiter to make each line just a single token:
    FOR /F "delims=" %%f In (list.txt) DO echo python script.py "%%f" >> newbatch.bat

    To be honest, I was leaving something for the OP to do rather than spoon feeding it to him; all this stuff is documented and I found it my first google search.
     

Share This Page

Advertisement: