Question:

A way of re-inputting the output of a command in a batch file?

by  |  earlier

0 LIKES UnLike

Hey I have a quick batch file question, I’m running the “ATTRIB picture.JPG” and that searches for the picture and when it’s found it just re-lists the file location of that file in the command prompt is there any way of copying the found file in a batch file? Because if I’m at the computer I can type “COPY file location” but if I’m away from the computer I need a way of having it running the attrib command and then a way of it re-inputting the output of the attrib command so it can automatically copy the found file…do you know how I could do that? Thanks for the help

 Tags:

   Report

1 ANSWERS


  1. You can save the location of the found file in a variable, I.E.

    @echo off

    set var=C:\somefile.txt

    copy C:\somefile.txt C:\somedir\

    Edit:

    This was a hard one, and one reason why i hate working with batch files, there are basically no useful information on the subject, and it doesn't seam that you can set a variable using a function I.E. set var = dir

    Nevertheless i still managed to figure out a method which works, I'm sure you can improve it on your own from here.

    Since setting a variable to the result of a function proved to be hard, i simply saved it in a text file, and then declared the variable. The syntax doesn't make much sense, i see no reason why it shouldn't be possible.

    Batch files may be useful to some degree, but they cant compete with a real scripting language such as AutoIt - http://www.autoitscript.com/autoit3/ AutoIt is also easier then working with batch files.

    -----StartCode-----

    dir C:\FileToFind.bat > currentfile.txt /B

    set /p var=<currentfile.txt

    copy C:\%var% C:\destination\

    pause

    -----CodeEnd-----

    Above will output the result from the dir command to a text.file, the "/B" at the end indicate that it should only save the result, so if you are searching for a bunch of random files, you can easily do something like below:

    -----StartCode-----

    @echo off

    set num=0

    :Loop

    SET /A num=%num%+1

    dir C:\FileToFind-%num%.bat > currentfile.txt /B

    set /p var=<currentfile.txt

    copy C:\%var% C:\destination\

    @GOTO Loop

    -----CodeEnd-----

    The above example is untested on my part, it might require that the destination dir exists, so you would want to make this first, or check for its existence.

    You can also set the drive letter dynamically, like i did with the filename, the above example should give you an idea of how to do this.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.