FOR command / batch including DIR

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

FOR command / batch including DIR

#1 Post by drgt » 04 Jan 2017 09:30

Please help me with the syntax of:

copy every file in dir c:\a\b\c /a-d /on /b to c: \d\e\f

both in command line and batch.

Alternatively, copy every file in c:\a\b\c\filelist.txt from c:\d\e\f to c:\g\h\i

Thank you

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: FOR command / batch including DIR

#2 Post by Compo » 04 Jan 2017 10:29

Possibly…

Code: Select all

Copy C:\a\b\c\*.* C:\d\e\f
…if you need something else please explain it better.

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: FOR command / batch including DIR

#3 Post by Sounak@9434 » 04 Jan 2017 11:42

For the second one use this(untested)

Code: Select all

for /f %%a in ("c:/a/b/c/filelist.txt") do (
 cd "c:/d/e/f"
 if exists %%a copy %%a "c:/g/h/i"
)


Sounak

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: FOR command / batch including DIR

#4 Post by Squashman » 04 Jan 2017 13:26

Sounak@9434 wrote:For the second one use this(untested)

Code: Select all

for /f %%a in ("c:/a/b/c/filelist.txt") do (
 cd "c:/d/e/f"
 if exists %%a copy %%a "c:/g/h/i"
)


Sounak

You might want to account for spaces in file names.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: FOR command / batch including DIR

#5 Post by drgt » 04 Jan 2017 22:34

Compo wrote:Possibly…

Code: Select all

Copy C:\a\b\c\*.* C:\d\e\f
…if you need something else please explain it better.


The difference between copy every file in dir c:\a\b\c /a-d /on /b to c: \d\e\f AND

Code: Select all

Copy C:\a\b\c\*.* C:\d\e\f
is that the latter will copy the files in "disk order" while the requirement is that they be copied in alphabetical order that the dir command specifies.

Of course, the dir command can be redirected to create a filelist.txt and then use that to carry out the copying in the order that the file dictates.
I thought there might be a way to skip the creation of the filelist.txt and do it all in one like

Code: Select all

for /f %%a in dir....

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: FOR command / batch including DIR

#6 Post by Sounak@9434 » 04 Jan 2017 22:45

Squashman wrote:
Sounak@9434 wrote:For the second one use this(untested)

Code: Select all

for /f %%a in ("c:/a/b/c/filelist.txt") do (
 cd "c:/d/e/f"
 if exists %%a copy %%a "c:/g/h/i"
)


Sounak

You might want to account for spaces in file names.

Thanks Squashman, new code(untested):-

Code: Select all

for /f "delims=" %%a in ("c:/a/b/c/filelist.txt") do (
 cd "c:/d/e/f"
 if exists "%%a" copy "%%a" "c:/g/h/i"
)


Sounak
Last edited by Sounak@9434 on 05 Jan 2017 01:26, edited 1 time in total.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: FOR command / batch including DIR

#7 Post by drgt » 04 Jan 2017 23:24

Sounak@9434 wrote:Thanks Squashman, new code(untested):-

Code: Select all

for /f "delims=" %%a in ("c:/a/b/c/filelist.txt") do (
 cd "c:/d/e/f"
 if exists %%a copy %%a "c:/g/h/i"
)


Sounak


Why not using backslashes?

Result (not related to above question):

Code: Select all

%a was unexpected at this time

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: FOR command / batch including DIR

#8 Post by Sounak@9434 » 05 Jan 2017 01:10

drgt wrote:Why not using backslashes?

You mean using backslash as delimiter?

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: FOR command / batch including DIR

#9 Post by drgt » 05 Jan 2017 01:24

I mean you use c:/a/b/c/filelist.txt instead of c:\a\b\c\filelist.txt

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: FOR command / batch including DIR

#10 Post by Sounak@9434 » 05 Jan 2017 02:15

drgt wrote:I mean you use c:/a/b/c/filelist.txt instead of c:\a\b\c\filelist.txt

Sorry, I should have posted it from my pc(android is tricky for typing codes) though yeah my fault. Fixed one here:-

Code: Select all

for /f "delims=" %%a in ("c:\a\b\c\filelist.txt") do (
 cd "c:\d\e\f"
 if exist "%%a" copy "%%a" "c:\g\h\i"
)

Thanks drgt for pointing that out.

Sounak

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: FOR command / batch including DIR

#11 Post by drgt » 05 Jan 2017 02:59

Sounak@9434 wrote:Thanks drgt for pointing that out.

Sounak


Never mind!

Lets see if someone has the answer to:
drgt wrote:The difference between copy every file in dir c:\a\b\c /a-d /on /b to c: \d\e\f AND

Code: Select all

Copy C:\a\b\c\*.* C:\d\e\f
is that the latter will copy the files in "disk order" while the requirement is that they be copied in alphabetical order that the dir command specifies.

Of course, the dir command can be redirected to create a filelist.txt and then use that to carry out the copying in the order that the file dictates.
I thought there might be a way to skip the creation of the filelist.txt and do it all in one like

Code: Select all

for /f %%a in dir....


***That is why the title is "For ... including DIR

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: FOR command / batch including DIR

#12 Post by Sounak@9434 » 05 Jan 2017 08:16

drgt wrote:I thought there might be a way to skip the creation of the filelist.txt and do it all in one like

Code: Select all

for /f %%a in dir....


***That is why the title is "For ... including DIR

In this case you are talking about the first request. Yeah the code can be achived by directly redirecting the output of dir command using 'singlequote'(maybe with usebackq and "double quote" for file name with spaces).

Sounak

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: FOR command / batch including DIR

#13 Post by drgt » 05 Jan 2017 23:52

Can you be more specific please?

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: FOR command / batch including DIR

#14 Post by Sounak@9434 » 06 Jan 2017 00:10

I am just replying based on the command you said earlier 'dir c:\a\b\c /a-d /on /b'

Code: Select all

for /f "delims=" %%a in ('dir c:\a\b\c /a-d /on /b') do (
   copy "%%a" "c:\d\e\f"
)

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: FOR command / batch including DIR

#15 Post by Compo » 06 Jan 2017 02:58

drgt wrote:The difference between copy every file in dir c:\a\b\c /a-d /on /b to c: \d\e\f AND

Code: Select all

Copy C:\a\b\c\*.* C:\d\e\f
is that the latter will copy the files in "disk order" while the requirement is that they be copied in alphabetical order that the dir command specifies.

If you are only performing a copy, and one which will happen relatively quickly, why would the order matter? If I was copying the content of a script on here, the end product would look exactly the same if copied it from bottom to top or the opposite.

Post Reply