copy files from uncertain subfolders into another location

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

copy files from uncertain subfolders into another location

#1 Post by goodywp » 07 Aug 2018 08:19

Hi,

I need to copy all files which sit from one subfolder to another location. The issue is that I do not know how many subfolder levels under one known folder (efg) downtwards to this final subfolder, which shall have files with extension .S3S, this final subfolder name can vary depend on different naming convertion...

For examples, I have some files sit in C:\abc\efg\...\1.S3S
C:\abc\efg\...\2.S3S
C:\abc\efg\...\3.S3S
C:\abc\efg\...\4.S3S
C:\abc\efg\...\5.S3S
I need to copy to C:\target folder\

Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: copy files from uncertain subfolders into another location

#2 Post by aGerman » 07 Aug 2018 09:34

Use DIR with options /B /S in a FOR /F loop or directly a FOR /R loop to iterate recursively across the folder structure.

Steffen

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

Re: copy files from uncertain subfolders into another location

#3 Post by Squashman » 07 Aug 2018 10:12

And you were shown how to do both of these concepts in two of your previous threads.

viewtopic.php?f=3&t=8100&p=53824#p53829
viewtopic.php?f=3&t=8715&p=57432#p57433

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: copy files from uncertain subfolders into another location

#4 Post by goodywp » 08 Aug 2018 06:13

Thank you both!!!
I tried this as below and works

Code: Select all

call C:\auto_pkg_build\Scripts\single_comp\srcfg.cmd
cd "C:\auto_pkg_build\Sources\Source_schemes\Tetra_Schemes\%VARSRC5%-*\%VARSRC5%"
FOR /F "delims=" %%G IN ('dir /b /s *.S3S') DO (
	copy %%G C:\auto_pkg_build\Sources\Source_schemes\Single_Replace\%VARSRC1%\
	)
But I was not able to do the same as below:

Code: Select all

call C:\auto_pkg_build\Scripts\single_comp\srcfg.cmd
cd "C:\auto_pkg_build\Sources\Source_schemes\Tetra_Schemes\%VARSRC5%-*\%VARSRC5%"
FOR /R %%G IN (*.S3S) DO IF EXIST "%%~dpnG" copy "%%~dpnG" C:\auto_pkg_build\Sources\Source_schemes\Single_Replace\%VARSRC1%\
Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: copy files from uncertain subfolders into another location

#5 Post by aGerman » 08 Aug 2018 09:26

What do you think are the modifiers ~dpn for?

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: copy files from uncertain subfolders into another location

#6 Post by goodywp » 08 Aug 2018 13:21

aGerman wrote:
08 Aug 2018 09:26
What do you think are the modifiers ~dpn for?

Steffen
expands %G to a drive letter and path only
tried using %~nxG expands %G to a file name and extension only not working :x

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: copy files from uncertain subfolders into another location

#7 Post by aGerman » 08 Aug 2018 13:52

~dpn expands
C:\bla\blub.txt
to
C:\bla\blub

A few more questions to you:
1) Will the file be found if the file extension was stripped from the file name?
2) What is the difference between the content of %%G in the FOR /F loop and the content of %%G in the FOR /R loop?

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: copy files from uncertain subfolders into another location

#8 Post by goodywp » 09 Aug 2018 06:51

aGerman wrote:
08 Aug 2018 13:52
~dpn expands
C:\bla\blub.txt
to
C:\bla\blub

A few more questions to you:
1) Will the file be found if the file extension was stripped from the file name?
2) What is the difference between the content of %%G in the FOR /F loop and the content of %%G in the FOR /R loop?

Steffen
FOR /F - Loop through items in a text file
FOR /R - Loop through files (recurse subfolders)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: copy files from uncertain subfolders into another location

#9 Post by aGerman » 09 Aug 2018 07:39

goodywp wrote:
09 Aug 2018 06:51
FOR /F - Loop through items in a text file
Not in your case. And you didn't answer my question of what you'll find in %%G. (hint: ECHO %%G helps to figure it out)

FWIW FOR /F processes streams. That can be file streams besides of the standard output stream or string streams. In your example the standard output stream of the DIR command will be processed. No text file is involved.

Steffen

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: copy files from uncertain subfolders into another location

#10 Post by goodywp » 22 Aug 2018 09:02

aGerman wrote:
09 Aug 2018 07:39
goodywp wrote:
09 Aug 2018 06:51
FOR /F - Loop through items in a text file
Not in your case. And you didn't answer my question of what you'll find in %%G. (hint: ECHO %%G helps to figure it out)

FWIW FOR /F processes streams. That can be file streams besides of the standard output stream or string streams. In your example the standard output stream of the DIR command will be processed. No text file is involved.

Steffen
You are right, there is another one for
FOR /F - Loop through the output of a command
Thanks a lot!

Post Reply