Page 1 of 1

copy files from uncertain subfolders into another location

Posted: 07 Aug 2018 08:19
by goodywp
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

Re: copy files from uncertain subfolders into another location

Posted: 07 Aug 2018 09:34
by aGerman
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

Re: copy files from uncertain subfolders into another location

Posted: 07 Aug 2018 10:12
by Squashman
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

Re: copy files from uncertain subfolders into another location

Posted: 08 Aug 2018 06:13
by goodywp
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

Re: copy files from uncertain subfolders into another location

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

Steffen

Re: copy files from uncertain subfolders into another location

Posted: 08 Aug 2018 13:21
by goodywp
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

Re: copy files from uncertain subfolders into another location

Posted: 08 Aug 2018 13:52
by aGerman
~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

Re: copy files from uncertain subfolders into another location

Posted: 09 Aug 2018 06:51
by goodywp
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)

Re: copy files from uncertain subfolders into another location

Posted: 09 Aug 2018 07:39
by aGerman
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

Re: copy files from uncertain subfolders into another location

Posted: 22 Aug 2018 09:02
by goodywp
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!