Copy file type from sub directories, into a folder renaming

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Copy file type from sub directories, into a folder renaming

#1 Post by pditty8811 » 22 Feb 2013 14:54

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.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

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

#2 Post by mfm4aa » 22 Feb 2013 16:11

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
)

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

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

#3 Post by pditty8811 » 22 Feb 2013 16:18

Are you sure? Or does that just not overwrite?

It's not renaming any copies.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

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

#4 Post by mfm4aa » 22 Feb 2013 16:36

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

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

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

#5 Post by shirulkar » 22 Feb 2013 16:55

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

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

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

#6 Post by pditty8811 » 22 Feb 2013 17:05

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.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

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

#7 Post by mfm4aa » 22 Feb 2013 17:09

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%\"
   )
)


pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

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

#8 Post by pditty8811 » 22 Feb 2013 17:10

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.

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

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

#9 Post by shirulkar » 22 Feb 2013 17:17

what is the full path of your bat file?

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

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

#10 Post by shirulkar » 22 Feb 2013 17:29

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

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

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

#11 Post by pditty8811 » 22 Feb 2013 17:32

1 sec please. I'm trying to figure some things out. I'll get back to you.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

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

#12 Post by pditty8811 » 22 Feb 2013 17:39

I did figure it out, I am using mfm method.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

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

#13 Post by pditty8811 » 22 Feb 2013 17:39

Thank you all!


Post Reply