You are not logged in.

#1 09 Jun 2007 00:10

Icecold
Member
Registered: 08 Jun 2007
Posts: 12

Files w/o subfolders

I have thousands of files in subfolders that I would like to copy into one folder can someone help. Please check my code and help me out.
My original files are in the C drive in a Test folder something like this

TestFolder
---------SubFolder
------------------SubFolder
----------------------------Files
---------SubFolder
------------------SubFolder
----------------------------Files
and so on

I would like to copy only all the files over to a FinishedTest folder without the subfolders... just the files.
If there are duplicates it just overwrites them -y

@echo off
set i = C:\Test
set k = C:\FinishedTest

for /d %%i in (%%n\*) do for /d %%j in (%%i\*) do for %%k in (%%j\*.jpg) do copy %%k %2

Offline

#2 09 Jun 2007 17:04

Fen_Li
Member
From: Malang, Indonesia
Registered: 04 May 2007
Posts: 22
Website

Re: Files w/o subfolders

May this helps:

if you want to copy all files (not a directory) in c:\test to c:\FinishedTest\..
..Untested..

for /f "tokens=*" %%c in ('dir c:\test /a:-d /s /b') do copy /y "%%c" "c:\FinishedTest"

nb:

@echo off
set i = C:\Test
set k = C:\FinishedTest

use %variable% to call a variable you have set, so if you want to call "c:\test" use %i% ,not  %%i
smileneutralsadbig_smileyikeswink

Last edited by Fen_Li (09 Jun 2007 17:25)


G-Smart thing Smart

Offline

#3 11 Jun 2007 16:44

Icecold
Member
Registered: 08 Jun 2007
Posts: 12

Re: Files w/o subfolders

It didn't work
I get %%c was unexpected at this time.
Help!!!!

Offline

#4 11 Jun 2007 19:05

Fen_Li
Member
From: Malang, Indonesia
Registered: 04 May 2007
Posts: 22
Website

Re: Files w/o subfolders

that script must be run in Batch File (*.bat)
if you run on CMD change %%c with %c

..TESTED and work fine..
----------------------------------------------------------------------------------------------
for /f "tokens=*" %c in ('dir c:\test /a:-d /s /b') do copy /y "%c" "c:\FinishedTest"


G-Smart thing Smart

Offline

#5 11 Jun 2007 19:20

Icecold
Member
Registered: 08 Jun 2007
Posts: 12

Re: Files w/o subfolders

Thanks that worked.
What if I wanted to run that script but when it copied the files to the FinishedTest folder it renamed the files with the first 7 characters of the filename and extension

Offline

#6 11 Jun 2007 20:03

Fen_Li
Member
From: Malang, Indonesia
Registered: 04 May 2007
Posts: 22
Website

Re: Files w/o subfolders

..TESTED and work...
----------------------------------------------------------------------
@echo off
if not exist "c:\test" echo Folder Missing && pause && goto :eof
if not exist "c:\FinishedTest" md "c:\FinishedTest"

REM copy all file(s) to target
for /f "tokens=*" %%c in ('dir c:\test\* /a:-d /s /b') do copy /y "%%c" "c:\FinishedTest"

REM rename all file(s) by first 7 char and extension in "c:\FinishedTest"
for /f "tokens=*" %%m in ('dir c:\FinishedTest /a:-d /s /b') do (
        REM set filename variable as filename only
        set filename=%%~nm

        REM set fullpath+filename as variable
        set fullname=%%m

        REM set extension variable as file.extension only
        set extension=%%~xm

        REM navigate to file directory
        cd /d "%%~dpm"
        call :next
)
echo Rename was Succesfull
pause>nul
exit
:next
REM set rename variable as get 1'st 7char of filename
set Rename=%filename:~0,7%

REM final step:Rename File
ren "%fullname%" "%rename%%extension%"

REM arrange duplicate file
if errorlevel 1 for /L %%l in (1,1,10) do ren "%fullname%" "%rename%(%%l)%extension%"

Last edited by Fen_Li (11 Jun 2007 22:49)


G-Smart thing Smart

Offline

#7 11 Jun 2007 20:34

Icecold
Member
Registered: 08 Jun 2007
Posts: 12

Re: Files w/o subfolders

The files copy over correctly but the rename does not work they do not get renamed.
I even corrected the typo
REM set reneme variable as get 1'st 7char of filename

to Rename

Offline

#8 11 Jun 2007 21:28

Fen_Li
Member
From: Malang, Indonesia
Registered: 04 May 2007
Posts: 22
Website

Re: Files w/o subfolders

Thnx for correction..
I've correct that script and work...

nb:

REM arrange duplicate file
if errorlevel 1 for /L %%l in (1,1,10) do ren "%fullname%" "%rename%(%%l)%extension%"

this script run if duplicate file found:
ex: file 1=hello Bull.txt --> first 7char=hello B
      file 2=hello Bill.txt --> first 7char=hello B
      file 3=hello Ball.txt --> first 7char=hello B
result :
file 1 renamed to "hello B.txt"
file 2 can't rename to hello B.txt because it was exist --> so, it renamed to hello B(1).txt
and file 3 to hello B(2).txt

BTW,native way: you can change:
if errorlevel 1 for /L %%l in (1,1,10) do ren "%fullname%" "%rename%(%%l)%extension%"
to
REM delete duplicate file were found
if errorlevel 1 for /L %%l in (1,1,10) do del /f /q "%fullname%

smileneutralsadbig_smile

Last edited by Fen_Li (11 Jun 2007 22:52)


G-Smart thing Smart

Offline

#9 11 Jun 2007 22:33

Icecold
Member
Registered: 08 Jun 2007
Posts: 12

Re: Files w/o subfolders

There is an error in the final step filename needs to be changed to rename

REM final step:Rename File
ren "%fullname%" "%filename%%extension%"

Here is the the correct one
ren "%fullname%" "%Rename%%extension%"

Offline

#10 12 Jun 2007 22:51

Icecold
Member
Registered: 08 Jun 2007
Posts: 12

Re: Files w/o subfolders

How could I set the length to some kind of variable
It Doesn't work if I do it like this it renames the file with a 7 instead of the length of the first 7 character.

Set /P length=7

REM set rename variable as get 1'st %length% char of filename
set Rename=%filename:~0,%%length%


REM set rename variable as get 1'st 7 char of filename
set Rename=%filename:~0,7

Last edited by Icecold (12 Jun 2007 23:32)

Offline

#11 13 Jun 2007 16:57

Fen_Li
Member
From: Malang, Indonesia
Registered: 04 May 2007
Posts: 22
Website

Re: Files w/o subfolders

ok, you must read this first :
http://ss64.com/nt/set.html

to evaluate variable, use a call command:
Use this in Batch File:

@echo off
set filename=yourownfile
Set length=7
REM set rename variable as get 1'st %length% char of filename
call set Rename=%%filename:~0,%length%%%
echo Result : %Rename%

Result will be:
--------------------
Result : yourown

nb:use symbol "^" to run that in console
     call set Rename=%filename:~0,^%length%%

Last edited by Fen_Li (13 Jun 2007 18:01)


G-Smart thing Smart

Offline

#12 21 Jun 2007 18:42

Icecold
Member
Registered: 08 Jun 2007
Posts: 12

Re: Files w/o subfolders

REM set rename variable as get 1'st 7char of filename
set Rename=%filename:~0,7%

REM final step:Rename File
ren "%fullname%" "%rename%%extension%"

REM arrange duplicate file
if errorlevel 1 for /L %%l in (1,1,10) do ren "%fullname%" "%rename%(%%l)%extension%"

Fen Li you have been great help.  Is there anywhere in here where I can check the 2nd and 3rd character from the right of the filename and if it is "Ma" delete the file or otherwise it can Rename it

Need all good files renamed except for filenames "Ma" bad files those should be deleted

Offline

#13 28 Jun 2007 17:27

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Files w/o subfolders

Check the "set /?" help ... there's a section about halfway through about working with sub-strings.


cmd | *sh | ruby | chef

Offline

#14 29 Jun 2007 18:26

Icecold
Member
Registered: 08 Jun 2007
Posts: 12

Re: Files w/o subfolders

Instead of doing a character count could I do find a string in the filename or would this only read the whole document and do a find.
find all filenames with "MA" and delete them.

FINDSTR /s /i MA *.*

Offline

#15 29 Jun 2007 18:57

Fen_Li
Member
From: Malang, Indonesia
Registered: 04 May 2007
Posts: 22
Website

Re: Files w/o subfolders

for %a in (*MA*) do del /f /q "%a"

are you mean that ?


G-Smart thing Smart

Offline

#16 29 Jun 2007 19:40

Icecold
Member
Registered: 08 Jun 2007
Posts: 12

Re: Files w/o subfolders

REM copy all file(s) to target
for /f "tokens=*" %%c in ('dir c:\test\* /a:-d /s /b') do copy /y "%%c" "c:\FinishedTest"

How would I do that here I tried this is it correct

REM copy all file(s) to target
for /f "tokens=*" %%c in ('dir c:\test\* /a:-d /s /b') do copy /y "%%c" "c:\FinishedTest"
for %%c in (*MA*) do del /f /q "%%c"  "C:\Test2"

Offline

#17 29 Jun 2007 19:53

Fen_Li
Member
From: Malang, Indonesia
Registered: 04 May 2007
Posts: 22
Website

Re: Files w/o subfolders

if file you want to delete is in "C:\FinishedTest" your code should be :
for %%c in (C:\FinishedTest\*MA*) do del /f /q "%%c"

del /f /q "%%c"  "C:\Test2"

what stands for "C:\Test2" ??

Last edited by Fen_Li (29 Jun 2007 19:58)


G-Smart thing Smart

Offline

#18 29 Jun 2007 20:01

Icecold
Member
Registered: 08 Jun 2007
Posts: 12

Re: Files w/o subfolders

Thanks Fen_Li exactly what I wanted it to do. Sorry about the Test2 my mistake

Offline

#19 30 Jun 2007 11:06

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Files w/o subfolders

Only trouble with using "*MA*" as your file selection string is that these would all match:

  "BadFileMan.txt"
  "BadFileMan"
  "BadManFile.doc"
  "BadManFile"
  "ManBadFile"
  "ManBadFile.xls"
  "GoodFileMa"
  "GoodFileMa.something"

And so forth...

For your purposes you might want to try "*Ma?" or similar (you may also have to take extensions into account with something like this: "*Ma?.???" or "*Ma?.*")


cmd | *sh | ruby | chef

Offline

#20 17 Sep 2007 17:26

psychosaif
Member
Registered: 17 Sep 2007
Posts: 3

Re: Files w/o subfolders

Iam trying to the do the same but move files older than 14 days. I have added the minage swich to Fen Li's code as follow:

@echo off

for /f "tokens=*" %%c in ('dir c:\a /a:-d /s /b /minage:14') do move /y "%%c" "c:\b"



However i get "Invalid Switch - minage:14" errors. Please help.

Offline

#21 17 Sep 2007 21:18

Fen_Li
Member
From: Malang, Indonesia
Registered: 04 May 2007
Posts: 22
Website

Re: Files w/o subfolders

ya, there is no /minage switch for dir command
you could try : copy & delete instead of Move

xcopy with "/D" switch
xcopy "source" "destination" /D:m-d-y
and then del "source\*.*"

more information : xcopy /?


G-Smart thing Smart

Offline

#22 17 Sep 2007 21:22

psychosaif
Member
Registered: 17 Sep 2007
Posts: 3

Re: Files w/o subfolders

thanks for the reply, but wont that copy and delete the subfolders older thatn 14 days as well? I am looking to delete just the files and maintain the folder structure.

thanks.

Offline

#23 17 Sep 2007 21:42

Fen_Li
Member
From: Malang, Indonesia
Registered: 04 May 2007
Posts: 22
Website

Re: Files w/o subfolders

Del /s /f /q "C:\a\*.*"
it just delete all file in subdir of "C:\A\" and keep folder structure

and you must modifty "/D:m-d-y" with  current date 14
ie : now is 9-18-2007
in script = 18 -14 =4
so, that script wiil be : xcopy "source" "destination" /S /D:9-4-2007

Last edited by Fen_Li (17 Sep 2007 21:43)


G-Smart thing Smart

Offline

#24 18 Sep 2007 15:29

psychosaif
Member
Registered: 17 Sep 2007
Posts: 3

Re: Files w/o subfolders

I am trying to automate this process and run the script every week and it looks like i will have to change the date in the script every week. The script i was using was as follow:

@echo off
robocopy c:\a c:\b /s /move /minage:14

However this deleted the folder structure as well. Is there anything i can do to modify the script above to delete files only? Can i use these switches Del /s /f /q "C:\a\*.*" for robocopy?

Offline

#25 19 Sep 2007 09:59

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Files w/o subfolders

I'd perhaps make robocopy copy the files only, save the robocopy result to a log file (you'll need to seriously reduce what it reports to get this file usable) and then use "for /f" to process that log file to delete the files that robocopy copied.

Alternatively, you could use robocopy to create a list of files only (you'll need lots of switches to produce the required output, and tidy it up into a usable form), process that output with "for /f" using the native "move" command to move files.

See post #4 on this thread for a script which demonstrates the latter concept (though that script is for deleting things.)

Last edited by bluesxman (19 Sep 2007 10:06)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by