Page 1 of 1

Copy file type from sub directories, into a folder renaming

Posted: 22 Feb 2013 14:54
by pditty8811
I'd like to copy one filetype from sub directories, placing the copies into one folder, renaming them so they don't overwrite. Here is my code now. It does everything I'd like except it overwrites files with same name:

Code: Select all

set dSource=C:\Users\P Ditty\Documents\SH3\data\cfg\Careers
set dTarget=C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR
set fType=*.clg
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
    copy  /v "%%f" "%dTarget%\" 2>nul
)

Thank you.

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 16:11
by mfm4aa

Code: Select all

for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do if not exist "%dTarget%\%%~nxf" (
    copy  /v "%%f" "%dTarget%\" 2>nul
)

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 16:18
by pditty8811
Are you sure? Or does that just not overwrite?

It's not renaming any copies.

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 16:36
by mfm4aa
pditty8811 wrote:...It's not renaming any copies.

Yes, try this:

Code: Select all

setlocal enabledelayedexpansion
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
   if exist "%dTarget%\%%~nxf" (
      copy  /v "%%~f" "%dTarget%\!random!!random!.clg"
   ) else (
      copy  /v "%%~f" "%dTarget%\"
   )
)


other suggestion you can find here: http://stackoverflow.com/questions/15033659/copy-file-type-from-sub-directories-into-one-folder-renaming-files-so-they-don

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 16:55
by shirulkar
Following will work just set Filepath. I left it empty. Filepath should be you .bat file path (Ex. C:\Users\P Ditty\Documents)

Code: Select all

@Echo OFF
set dSource=C:\Users\P Ditty\Documents\SH3\data\cfg\Careers
Set dTarget=C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR
Set Filepath=

for /f "tokens=* delims=" %%f in ('dir /a-d /b /s "%dSource%\*.txt"') do call :Fun1 "%%~nxf"

Exit /b

:Fun1
set Str1="%~1"
cd %dTarget%
If Exist %Str1% ( Echo file alredy present
) ELSe ( copy %dSource%\%str1% "%dTarget%\")
cd %Filepath%

Exit /b

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 17:05
by pditty8811
I can't do the filepath or copy folder tree unless I change this code too:

Code: Select all

>Campaign_SCR.mis.tmp (
    for /f "tokens=1* delims=:" %%A in ('findstr /n "^" Campaign_SCR.mis.backup') do (
        ( echo !ln!| findstr "^Type=12$" >NUL && set ln=ln ) || (
            set "ln=%%B"
            if "!ln:~0,6!"=="Class=" (
                findstr /s /c:"ClassName=!ln:~6!" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR\*.clg" >"C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR\null" && (
                    echo Class=ShipDummy
                    set "ln=Type=12"
                )
            )
            if #!ln!==# (echo;) else echo !ln!
        )
    )
)


That only searches in Backups_SCR for .clg not its subdirectories. So unless I change that. It would be best to just give it a random number.

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 17:09
by mfm4aa
You can also check, if the file with the random number already exists:

Code: Select all

setlocal enabledelayedexpansion
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
   echo %%f
   if exist "%dTarget%\%%~nxf" (
      :loop
      set "tname=%dTarget%\!random!.clg"
      if exist !tname! goto :loop
      copy  /v "%%~f" "!tname!"
   ) else (
      copy  /v "%%~f" "%dTarget%\"
   )
)


Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 17:10
by pditty8811
mfm4aa wrote:You can also check, if the file with the random number already exists:

Code: Select all

setlocal enabledelayedexpansion
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
   echo %%f
   if exist "%dTarget%\%%~nxf" (
      :loop
      set "tname=%dTarget%\!random!.clg"
      if exist !tname! goto :loop
      copy  /v "%%~f" "!tname!"
   ) else (
      copy  /v "%%~f" "%dTarget%\"
   )
)




Is that the full search code? Where do I put the source, filetype and destination?

Sorry I'm learning, but I'm learning fast. Thank you.

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 17:17
by shirulkar
what is the full path of your bat file?

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 17:29
by shirulkar
This will work for you

Code: Select all

@Echo OFF
cd > a.txt
set /p "abc="< a.txt
set dSource=C:\Users\P Ditty\Documents\SH3\data\cfg\Careers
Set dTarget=C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR

for /f "tokens=* delims=" %%f in ('dir /a-d /b /s "%dSource%\*.txt"') do call :Fun1 "%%~nxf"
cd %abc%
del a.txt

Exit /b

:Fun1
set Str1="%~1"
cd %dTarget%
If Exist %Str1% ( Echo file alredy present
) ELSe ( copy %dSource%\%str1% "%dTarget%\")


Exit /b

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 17:32
by pditty8811
1 sec please. I'm trying to figure some things out. I'll get back to you.

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 17:39
by pditty8811
I did figure it out, I am using mfm method.

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 17:39
by pditty8811
Thank you all!

Re: Copy file type from sub directories, into a folder renam

Posted: 22 Feb 2013 17:40
by shirulkar
Great :D