1

So, I have this code:

for /r ../ %%a IN (*.pdf) do move "%%a" ".\"

and I am trying to move all of the PDF files from the parent folder and not the parent's subfolders. For example,

My current directory is Child1 and I have this

Parent/Child1

Parent/Child2/Some-PDF-File.PDF

Parent/Child3/Another-PDF-File.PDF

Parent/PDF1.PDF

Parent/PDF2.PDF

Parent/PDF3.PDF

I am trying to move only files PDF1.PDF, PDF2.PDF, and PDF3.PDF to Child1. However, my code moves all of the PDF files.

What do I need to do?

Thanks.

P.S. This is part of my batch file

ECHO

for %%a in (..\*.pdf) do (
    move "%%a" .
    )

pause

for %%i in (*.pdf) do (
    pdftotext -enc UTF-8 "%%i" ".\output\%%~ni.txt"
    )

cd .\Output

findstr /S /I ## *.txt > ../log.txt

pause

cd ..

for %%F in ("log.txt") do (
    if %%~zF equ 0 (
        del "%%F"
        cd .\Output
        for /F "delims=" %%j in ('dir /b') do (
            del "%%j" /s/q
        )
    )
)

pause

for %%a in (*.pdf) do (
    move "%%a" ..
    )
1

However, my code moves all of the PDF files.

for /r ../ %%a IN (*.pdf) do move "%%a" ".\"

What do I need to do?

In the for command above you have specified /r, which means the for command is recursive - Loop through files (Recurse subfolders).

There are many different solutions to this issue but one would be the following batch file:

test.cmd:

@echo off 
setlocal enabledelayedexpansion
for %%a in (..\*.pdf) do (
  move "%%a" .
  )
endlocal

usage:

F:\test\Child1>dir ..\*.pdf
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

27/08/2016  11:06                 0 1complete1.pdf
27/08/2016  11:06                 0 2complete2.pdf
27/08/2016  11:06                 0 complete1.pdf
27/08/2016  11:06                 0 complete2(s).pdf
               4 File(s)              0 bytes
               0 Dir(s)  1,764,571,938,816 bytes free

F:\test\Child1>test
        1 file(s) moved.
        1 file(s) moved.
        1 file(s) moved.
        1 file(s) moved.

F:\test\Child1>dir
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test\Child1

16/09/2016  12:53    <DIR>          .
16/09/2016  12:53    <DIR>          ..
27/08/2016  11:06                 0 1complete1.pdf
27/08/2016  11:06                 0 2complete2.pdf
27/08/2016  11:06                 0 complete1.pdf
27/08/2016  11:06                 0 complete2(s).pdf
16/09/2016  12:53               102 test.cmd
               5 File(s)            102 bytes
               2 Dir(s)  1,764,571,938,816 bytes free
  • David, thank you for your help. Using the code similar to the one in test.cmd, how would you go about moving those files back to their original directory? – Ramin Melikov Sep 16 '16 at 13:50
  • @RaminMelikov for %%a in (*.pdf) do ( move "%%a" .. ) or move *.pdf .. – DavidPostill Sep 16 '16 at 13:52
  • David, thanks again. When I combine it in my batch file, it doesn't work :( – Ramin Melikov Sep 16 '16 at 14:01
  • I edited my original post to include the batch file. – Ramin Melikov Sep 16 '16 at 14:04
  • <shrug> Where exactly is it going wrong? – DavidPostill Sep 16 '16 at 16:53
0
cd /path/to/parent
mv *.pdf Child1

or just

mv /path/to/parent/*.pdf /path/to/parent/Child1

With mv, if you don't specify recurse, it doesn't recurse.

Since it's brought to my attention that the original 'code' is in batch,

cd <drive>:\path\to\parent
move *.pdf Child1

or

move <drive>:\path\to\parent\*.pdf <drive>:\path\to\parent\Child1

or run the first posted code in PowerShell, with the path in windows syntax.

No matter which way, no matter which OS and shell, OP's running a recursive command and gets a recursive result. If OP wants the contents of one folder, OP needs to move there and execute 'locally', execute with a relative path, or execute with an absolute path. If the execution of the command has to be from deeper in the filesystem than the source and the destination, then OP should use absolute paths, since relative paths get ugly when you try to go 'up'.

  • Please read the question again carefully. Your answer does not answer the original question. OP is clearly using Windows not Unix. – DavidPostill Sep 16 '16 at 11:42
  • It's not 'clear' what OP's using. OP never stated it. The code is cloudly and the only real clue is that 'move' is spelled out. Easy to miss. My suggested code works fine in PowerShell. In any case, I modified my answer to present the simplest solution. Also, there's the issue of using a loop wrapped around a command that will do the job perfectly using wildcards. Sledgehammers and flies and all that. – Xalorous Sep 16 '16 at 13:14

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.