Batch file to move files to local system

Microsoft Windows xp professional (upgr...
June 22, 2010 at 08:53:05
Specs: Windows XP
Hi,

M new to batch file scripting. I need to write a batch file which moves files from our shared folder,and all sub folders (which resides on windows xp system in our lan) to my local system. It should move only those files that match a pattern (.doc) and are created in 2007. I have no option of going for perl or so and it need to be implemented as batch file only. Any help would be great.


See More: Batch file to move files to local system

Report •

#1
June 22, 2010 at 13:34:41
I have no option of going for perl or so and it need to be implemented as batch file only.
What's wrong with VBScript?

Report •

#2
June 22, 2010 at 18:13:18
@echo off
for /f "tokens=5*" %%a in ('dir shared\*.doc /s /t:c /a-d ^|find "/2007 "') do copy "%%a" .

Report •

#3
June 22, 2010 at 22:59:12
Hi nbrane,

For a simple test, I modified the line as
for /f "tokens=5*" %%a in ('dir C:\"Documents and Settings"\"My Documents"\Test\*.* s /s /t:c /a-d ^|find "/2010"') do copy "%%a"

And created a small txt file in the speicfied path. When the script was executed..it correctly picked that filename(turned echo on) but then says "System cannot find file specified"(as shown below the message". Tried with directory without spaces still there is something I am missing. Little Googling did not help. Any clue?

---------------------------------------------------------------------------------------

C:\Documents and Settings\Desktop>b.bat

C:\Documents and Settings\Desktop>for /F "tokens=5*" %a in ('dir C:\"Docu
ments and Settings"\"My Documents"\Test\*.* s /s /t:c /a-d |find "/2010"'
) do copy "%a"
File Not Found

C:\Documents and Settings\Desktop>copy "TestForBatchfile.txt"
The system cannot find the file specified.

C:\Documents and Settings\Desktop>


Report •

Related Solutions

#4
June 23, 2010 at 00:55:20
apologies are in order, and are offered here-bye!
I tried to cut shorts, and got tangled in the barbed wire.
I think it's cause the full path is not being delivered.
try this mod:
for /f "tokens=5*" %%a in ('dir C:\"Documents and Settings"\"My Documents"\Test\*.* /s /t:c /a-d ^|find "/2010"') do copy "%%~dpnxa" con
::---- end
(removed an apparently spurious "s" from your posted script)
Anyway, long story short, i was trying to skim by without a-lot-a date BS, since you only needed the year, but you needed "creation" and not the default "last mod" date.
Hope this comes closer to the mark - if not, as Razor suggested, vbscript has excellent tools to work with.

Report •

#5
June 23, 2010 at 04:25:39
Hi nbrane,

It still gives the same error. I will try for some more time and if could not, will try alternative scripts. Thanks a lot for your support. Its my first ever post on net regarding technical clarification. Nice start :-)


Report •

#6
June 23, 2010 at 07:18:24
Here's the VBScript version:
Const src = "\\our\shared folder"
Const dest = "C:\blackmail\some folder"
Const year = 2007

WScript.Quit Main
Function Main
  Dim cnt, fso
  Set fso = CreateObject("Scripting.FileSystemObject")
  cnt = 0 + DoCopy(fso, fso.GetFolder(src), dest)
  WScript.Echo "Total files copied: " & cnt
End Function

Function DoCopy(oFso, dSrc, sDest)
  On Error Resume Next
  Dim f, d, cnt, madeDir
  madeDir = Not oFso.FolderExists(sDest)
  If madeDir Then _
    oFSo.CreateFolder(sDest)
  
  'Copy files
  For Each f In dSrc.Files
    If LCase(oFso.GetExtensionName(f)) = "doc" And _
       DatePart("yyyy", f.DateCreated) = year Then
      f.Copy(sDest & "\")
      cnt = cnt + 1
    End If
  Next 'f
  
  'Walk directory tree
  For Each d In dSrc.SubFolders
    cnt = cnt + DoCopy(oFso, d, sDest & "\" & d.Name)
  Next 'd
  
  'Delete directory if empty and we made it
  If madeDir Then
    With oFso.GetFolder(sDest)
      If (.Files.Count = 0 And .SubFolders.Count = 0) Then
        .Delete
      End If
    End With
  End If
  DoCopy = cnt
End Function


Report •

#7
June 23, 2010 at 09:03:07
I'm curious about the internal double quotes within the dir
command. Would changing this:

for /f "tokens=5*" %%a in ('dir C:\"Documents and
Settings"\"My Documents"\Test\*.* /s /t:c /a-d ^|find "/2010"')
do copy "%%~dpnxa" con
::---- end

to this:
for /f "tokens=5*" %%a in ('dir "C:\Documents and
Settings\My Documents\Test\*.*" /s /t:c /a-d ^|find "/2010"') do
copy "%%~dpnxa" con
::---- end

make any difference?

Insomniac at large


Report •

#8
June 23, 2010 at 09:10:04
orangeboy: Would changing this make any difference?
Not for XP, no:
H:\>dir c:\"Documents and Settings"\"all users" /b
Application Data
Dell
Desktop
Documents
DRM
Favorites
Start Menu
Templates
NTUSER.DAT
NTUSER.DAT.LOG
ntuser.pol


Report •

#9
June 23, 2010 at 10:15:27
this fixed it i think (tested). I was once again a victim of spacey filenames:
@echo off
for /f "tokens=4*" %%a in ('dir "c:\documents and settings\my documents\test\*.doc" /s /t:c /a-d ^|find "/2007 "') do echo copy "%%b" c:\work


Report •

#10
June 25, 2010 at 04:57:39
Hi nbrane,

Apologies for not getting back soon as I was not well for last 2 days.

Tried this last line with little customization for my test. This time it eliminated errors but does not move the file(though it correctly traces through the source folder/subfolder files).
Created 'Source' directory,'Subfolder1'(under Source),test1.txt under Subfolder and test2.txt under Source directly. Created a 'Destination' folder and the following line was executed.

for /f "tokens=4*" %%a in ('dir "C:\Documents and Settings\My Documents\Source\*.txt" /s /t:c /a-d ^|find "/2010"') do echo copy "%%b" "C:\Documents and Settings\My Documents\Destination"

for which output is

C:\Documents and Settings\Desktop>e.bat
copy "test2.txt" "C:\Documents and Settings\My Documents\Destination"
copy "test1.txt" "C:\Documents and Settings\My Documents\Destination"

There are no space or access issues for 'Destination' folder.
Any clue?


Report •

#11
June 25, 2010 at 09:00:33
Sorry to hear you've been laid up... I also have some health issues so my sympathies.

It was set up to run in "safe mode".
did you remove the echo for a "live" test? From:

...2010"') do echo copy "%%b" "C:\Documents and Settings\My Documents\Destination"
to:
...2010"') do copy "%%b" "C:\Documents and Settings\My Documents\Destination"


Report •

#12
June 26, 2010 at 10:44:59
Hi nbrane,

removing echo did not help.Observed one thing with the above execution. When the script is run, its tracing through the source directory and sub-directory properly(this is evident from the list of files its trying to copy) but finally when it has to copy its expecting those files to be present at present working directory, in my case its Desktop. When I tried by inserting cd 'Source', before the commond is executed, the file under source gets copied but not the file under subfolder. I am not sure of how we can dynamically change directories in dos. Am I making any sense?


Report •

#13
June 26, 2010 at 11:40:47
perfect sense. I should have seen this problem coming up.
need one more level to capture the fullpath of the sourcefiles:
@echo off & setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir "c:\documents and settings\my documents\test\*.doc" /b /s /a-d') do (
dir /t:c %%a | find "/2007 ">nul & echo copy "%%a" "c:\temp files work\" & copy "%%a" "c:\temp files work\"
)
::------- end
it could be done as a "one-liner", but it gets harder to read that way.
I'm not sure what all your testing paths, extension and limit-date are set to, but i figure by now you know what you need to adjust for testing in your local environment.
remove the: >nul in line 3 if you want to see the FIND output.
This one will run slower due to the extra layer to capture the create-date. If too slow, you can always go the vbscript route for which R2 has already kindly provided the code.
hope this gets us there! ;-o

Report •

#14
June 27, 2010 at 00:12:52
That's marvelous.It worked. Thanks nbrane and R2 for your great support all through this journey. I will add up all my other requirements on top of this to achieve my task.
Thanks once again.

Report •

#15
June 28, 2010 at 07:15:52
Hi nbrane,

Today while doing extensive testing, found that its copying files of all years, ie.even if I specify 2007, its copying files of 2008/09/10. Is there a way around to avoid this. And by any chance is it possible to copy the files along with the folder structure?.


Report •

#16
June 28, 2010 at 09:44:00
How'd the VBScript work?

Report •

#17
June 28, 2010 at 15:57:03
ah, how did i miss the double &&???

for /f "tokens=*" %%a in ('dir "c:\documents and settings\my documents\test\*.doc" /b /s /a-d') do (
dir /t:c %%a | find "/2007 ">nul && echo copy "%%a" "c:\temp files work\"
)
::-------
just double up those ampersands in the mainline, remove "echo" safety, and it should fly.
vbscript is a very "friendly" language. At first i didn't like it, but after learning more and seeing the similiarities to visbasic I have concluded that it's "the BOMB".
If this is intended for heavy/repeated use, I would try both routes, time them, and pick the fastest (most likely vbscript).


Report •

Ask Question