Batch File - For Command??


  1. Posts : 2,177
    Windows 8.1 Pro x64
       #1

    Batch File - For Command??


    <This post has now been solved>


    Hi,

    Sorry if this is not the place but does anyone know how to do the following?

    I am trying to create a batch file to search a directory for a specified file type and list it in a text file then get the batch file to read that text file and copy all the files it found to a directory.

    i have created the following which does work... the problem is, when there is a space in the filename it says it cant find the file, i don't know how to get it to include spaces i would normally by using " ??

    Code:
    dir /s /b c:\source\*.txt >c:\files2copy.txt
    for /f %%i in (c:\files2copy.txt) do xcopy %%i %%j %%k c:\dest
    Any help will be greatly appreciated.
    Last edited by JDobbsy1987; 14 Sep 2010 at 14:33.
      My Computer


  2. Posts : 83
    Windows 7 Pro 64
       #2

    Hi JDobb, is there any reason that you cant do a two step process? Like create your list, then go back and have XCOPY work on the original directory. XCOPY is works fine for me with spaces in the file names, and it is designed to work on multiple files, where it looks like you are using it on individual files in the for loop.

    xcopy "*.txt" J:\test\test2\*.*

    here is a backup file that I use now and then
    Code:
    @echo off
    @echo Process saver run history
    @type 00SaveProcesses.log
    
    echo.
    echo this will backup the "Processes" folder to the primary backup (\\LEN\L)
    pause
    xcopy c:\users\PB\datapart\Processes\*.* \\len\Processes\*.* /s /d /y /f /c
    ::/y suppress overwrite prompting
    ::/f display full file names as copying
    ::/c continues on error
    ::/s subdirectories  
    ::/d only newer files
    echo complete %date% --- %time% >> 00SaveProcesses.log
    I have several other varations of this basic idea if you work best from examples.

    Caution, while you are working this out, Use the /L flag on XCOPY to check that it is going to work before running, or in your for loop
    Code:
    do @echo xcopy %%j ...
    to show what it is going to do before you do it. Remember that the commandline has no pity, and it will eat your lunch without asking.

    If you want to use the FOR loop, check in help where it describes the /F "options" part. You have to be careful with the "delims=..." section to get it right.

    and be especially careful using batch code that you find on the internet
      My Computer


  3. Posts : 2,177
    Windows 8.1 Pro x64
    Thread Starter
       #3

    Hi Periboob,

    Thanks for your reply...

    I don't want to copy the .txt file it self, i want my batch file to look inside the .txt file and copy the files listed in the .txt file.

    e.g.
    My .txt file contains:
    c:\test\test.txt
    c:\new folder\new.txt
    c:\old\jamie.txt

    so i want my batch file to then go to each of the above files and copy them to my output dir.

    Thanks,
    JDobbsy1987
      My Computer


  4. Posts : 83
    Windows 7 Pro 64
       #4

    It is in the "options" control for the FOR loop
    Try this for example

    Here is my xlist file

    Code:
    J:\Processes\test\rename>type xlist.txt
    J:\Processes\test\rename\x 1.txt
    J:\Processes\test\rename\xlist.txt
    J:\Processes\test\rename\y1.txt
    J:\Processes\test\rename\Y2.txt
    J:\Processes\test\rename\Y3.txt
    and here is the results using my tokens/delims string. Any special characters in the file names may change the results.
    Code:
    J:\Processes\test\rename>for /f "tokens=1* delims= " %i in (xlist.txt) do @echo copy "%i %j" k:\test\
    copy "J:\Processes\test\rename\x 1.txt" k:\test\
    copy "J:\Processes\test\rename\xlist.txt " k:\test\
    copy "J:\Processes\test\rename\y1.txt " k:\test\
    copy "J:\Processes\test\rename\Y2.txt " k:\test\
    copy "J:\Processes\test\rename\Y3.txt " k:\test\
    I just fumble around with this stuff, not a guru, but as I understand it, the "tokens=1*" part means that your first variable is as expected, and any following %variable like %j gets all the rest of the tokens delimited by spaces ("delims= ") that space between the quote and the paren is the delimeter between variables.

    Keep it test folders until you are confident what the results are likely to be. Periboob is not responsible if you fill up your hard drive in a recursive file copy. :)

    I probably should not be helping you keep doing things the old ways, I understand Microsoft is de emphasizing the commandline in favor of the newer scripting tools. But I am too old to learn new tricks.
      My Computer


  5. Posts : 2,177
    Windows 8.1 Pro x64
    Thread Starter
       #5

    periboob said:
    It is in the "options" control for the FOR loop
    Try this for example

    Code:
    J:\Processes\test\rename>for /f "tokens=1* delims= " %i in (xlist.txt) do @echo copy "%i %j" k:\test\
    copy "J:\Processes\test\rename\x 1.txt" k:\test\
    copy "J:\Processes\test\rename\xlist.txt " k:\test\
    copy "J:\Processes\test\rename\y1.txt " k:\test\
    copy "J:\Processes\test\rename\Y2.txt " k:\test\
    copy "J:\Processes\test\rename\Y3.txt " k:\test\
    Thank you...

    this example helped a lot and i have managed to do it.

    Regards,
    JDobbsy1987
      My Computer


  6. Posts : 83
    Windows 7 Pro 64
       #6

    JDobbsy1987 said:
    ...
    Thank you...
    ...
    Happy to help.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 03:18.
Find Us