2

I have a drive with several thousands of pictures on it. I've copied it all to another drive in which I've been organizing them slowly over several months. I've discovered a ton of corrupted files but found that the on the original drive, they aren't corrupted.

What I'd like to do is have a batch file that will read filenames from a text file and search the original drive for the filename, then copy it to a specified folder.

Since the original drive is unsorted, there could be duplicates so I'd like to copy all of the copies of that filename to the new folder. I have a script that might do it (see below) but it doesn't seem to be working because of the long filenames. For example, "2008-06-27 02.06.37.jpg" ... the script will search for "2008-06-27" instead of the full filename.

Any thoughts on how to fix this?

Here is my work that I have so far that's still not working as expected:

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

cls

set dest=F:\ERRORS\recovered

for /f %%f in (F:\ERRORS\errorlist.txt) do (

    set i=1

    for /f "tokens=*" %%F IN ('dir /S /B /A:-D "%%f"') Do (

        for %%N in ("%%F") Do (

            set name=%%~NN

            set ext=%%~XN

        )

        copy "%%F" "%dest%\!name!_!i!!ext!"

        set /A i=!i!+1

    )

)

ENDLOCAL
  • So what is the error? What doesn't it do that it should? – DavidPostill Mar 5 '17 at 19:02
  • instead of searching for "2008-06-27 02.06.37.jpg" ... it only reads "2008-06-27" and finds nothing. Once it hits the space in the filename its not reading the name past that spot. – JMasters Mar 5 '17 at 19:16
  • Still no luck. The problem seems to be in the line "for /f "tokens=*" %%F IN ('dir /S /B /A:-D "%%f"')" whereas %%f is only reading part of the filename from the text and not the whole name because of the space in the filename. – JMasters Mar 5 '17 at 20:09
  • Please edit your question and add a small piece of the F:\ERRORS\errorlist.txt file (2 to 4 lines should suffice). – JosefZ Mar 5 '17 at 20:19
1

You should add the "TOKENS=*" to the FOR /F loop that reads from the file list to ensure that lines read from within the file list which have spaces between literal characters are not interpreted as a delimiter or new line thus cutting off the character string iterated at the space and not getting the whole line of characters you need it to loop through including any spaces before carriage return or line feeds.

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION
cls

set dest=F:\ERRORS\recovered
for /f "TOKENS=*" %%f in (F:\ERRORS\errorlist.txt) do (
    set i=1
    for /f "tokens=*" %%F IN ('dir /S /B /A:-D "%%~f"') Do (
        for %%N in ("%%F") Do (
            set name=%%~NN
            set ext=%%~XN
        )
        copy "%%~F" "%dest%\!name!_!i!!ext!"
        set /A i=!i!+1
    )
)
ENDLOCAL

Further Resources

  • FOR /?

       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.
    
  • JMasters - I added this as an answer for you but this should resolve the problem just as you described in the comment clarifications conversation. I was able to recreate the problem and resolve it using this method of the FOR /F "TOKENS=*". Please keep the XCOPY command I mentioned in mind with that same syntax if you still need it too. – Pimp Juice IT Mar 5 '17 at 20:34
  • That's exactly what I was looking for! Thank you all so much for the help! :) – JMasters Mar 5 '17 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.