1.

Solve : Can this be done??

Answer»

I have to move a file (I'll call it file.txt) from one folder to another on approximately 500 PCs. I'm wondering if a batch file can be written that could be run on each PC by the user rather than me have to do each one manually. three folders on each PC contain a file.txt and each one is a different size

c:\profiles\folder1\file.txt
c:\profiles\folder2\file.txt
c:\profiles\folder3\file.txt

I need to copy the largest of the 3 to another location. The problem is that folder1, folder2, folder3 are going to be named differently on every PC. Is it possible to write a batch file that can look into all subfolders of c:\profiles and copy the largest file.txt to another location? Adjust the paths/filenames to suit - not tested, no allowance made for paths/filenames containing spaces. Hope it helps.

Code: [Select]@echo off>%temp%\dir.txt
cls

setlocal

dir /s /b c:\profiles\file.txt >%temp%\dirfil.txt

for /f "delims=*" %%1 in (%temp%\dirfil.txt) do (
echo %%~zf1>>%temp%\dir.txt
)

sort /R < %temp%\dir.txt > %temp%\dirsorted.txt

for /f "tokens=2" %%1 in (%temp%\dirsorted.txt) do (
copy %%1 Outfilepath\ >nul & goto :end
)

:end
exit/b

Good luck.Quote from: T.C. on January 11, 2010, 04:01:11 AM

Adjust the paths/filenames to suit - not tested, no allowance made for paths/filenames containing spaces. Hope it helps.

Code: [Select]@echo off>%temp%\dir.txt
cls

setlocal

dir /s /b c:\profiles\file.txt >%temp%\dirfil.txt

for /f "delims=*" %%1 in (%temp%\dirfil.txt) do (
echo %%~zf1>>%temp%\dir.txt
)

sort /R < %temp%\dir.txt > %temp%\dirsorted.txt

for /f "tokens=2" %%1 in (%temp%\dirsorted.txt) do (
copy %%1 Outfilepath\ >nul & goto :end
)

:end
exit/b

Good luck.
It will just crash. One thing I noticed in the for loop, %%1? It needs to be a letter (uppercase and lowercase are different). And what's with @echo off>%temp%\dirfill.txt? It's unnessasary. That file gets created in the dir command anyway. Quote from: Helpmeh on January 11, 2010, 04:40:06 AM
One thing I noticed in the for loop, %%1? It needs to be a letter

It is an undocumented fact that you can use %%0 to %%9 and some other characters too; (? # $ _ for example) although the FOR documentation says a-z and A-Z and says you can only have 52 variables existing at once.

Code: [Select]for /f "tokens=1-3 delims= " %%0 in ("cat DOG horse") do @echo %%0 %%1 %%2
Code: [Select]cat dog horse


Quote from: T.C. on January 11, 2010, 04:01:11 AM
Adjust the paths/filenames to suit - not tested, no allowance made for paths/filenames containing spaces. Hope it helps.

Code: [Select]@echo off>%temp%\dir.txt
cls

setlocal

dir /s /b c:\profiles\file.txt >%temp%\dirfil.txt

for /f "delims=*" %%1 in (%temp%\dirfil.txt) do (
echo %%~zf1>>%temp%\dir.txt
)

sort /R < %temp%\dir.txt > %temp%\dirsorted.txt

for /f "tokens=2" %%1 in (%temp%\dirsorted.txt) do (
copy %%1 Outfilepath\ >nul & goto :end
)

:end
exit/b

Good luck.

That works just like i need it too but when i try to change the path from c:\profiles to the actual path where the file RESIDES I'm having trouble. the filename is actually user.aco (not file.txt as i said earlier) and it exists in these folders:

C:\Documents and Settings\All Users\Application Data\myco\MQ1\Cache\User Profiles\92606\7354904
C:\Documents and Settings\All Users\Application Data\myco\MQ1\Cache\User Profiles\92689\8459875
C:\Documents and Settings\All Users\Application Data\myco\MQ1\Cache\User Profiles\123456\6546544

The last 2 numeric folders are going to be diferent on each PC and the largest user.aco needs to be copied to c:\program files\myco


Thanks for the help so far. Could you help me get the rest of the way?



Quote from: T.C. on January 11, 2010, 04:01:11 AM
Adjust the paths/filenames to suit - not tested, no allowance made for paths/filenames containing spaces. Hope it helps.


Good luck.
is there a way to make allowance for path containing spaces? Forgive me for being so dumb. DOS is definitely not my strong suit but it would be a huge help if I could get this worked out.Quote from: aschrier on January 11, 2010, 08:08:46 PM
is there a way to make allowance for path containing spaces?

Quote marks

"C:\Path with\some spaces\"

Quote from: helpmeh
It will just crash. One thing I noticed in the for loop, %%1? It needs to be a letter (uppercase and lowercase are different). And what's with @echo off>%temp%\dirfill.txt? It's unnessasary. That file gets created in the dir command anyway.

FYI Although it's undocumented, in a For loop the variable can be any printable ASCII/Extended ASCII code in the range dec 33 thru dec 254.

In a testing environment it is wise to ensure that any file created using redirection >> is deleted, otherwise, if the script is run more than once, data will be appended rather than the file being created with new data. @echo off>%temp%\dirfill.txt is just one way of overwriting the existing file with a zero length file. One could also use If Exist Filename Del Filename, or simply Del Filename when that file is no longer required, or any other method of removing the file.

According to the OP Quote from: aschrier
That works just like i need it too
the script did not "just crash" but appears to have been helpful.

aschrier - Salmon Trout has already advised on paths/filenames containing spaces.

Again - Good Luck.
Quote from: T.C. on January 12, 2010, 12:43:26 AM
FYI Although it's undocumented, in a For loop the variable can be any printable ASCII/Extended ASCII code in the range dec 33 thru dec 254.

These are the ONES you can't use

34 37 38 44 59 60 61 62 124

Method

Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")
Set Shell = CreateObject("Wscript.Shell")
For c = 33 To 255
WriteFileName="test" & CStr(c) & ".BAT"
Set TextFile = fso.OpenTextFile (WriteFileName, 8,True)
Textfile.WriteLine("@for /f " & Chr(34) &"delims= " & chr(34) & " %%" & Chr(c) & " in (" & chr(34) & "cat Dog horse" & chr(34) & ")" & " Do @Echo %1 %%" & Chr(c) & ">> report.txt")
Textfile.Close
Shellstring="%comspec% /c " & WriteFileName & " " & CStr(c)
Shell.run ShellString,2,1
Next
Set fso = Nothing
Set Shell = Nothing


Quote from: S T
These are the ones you can't use

34 37 38 44 59 60 61 62 124

Did you try Escaping the chars?

Code: [Select]@echo off
setlocal
cls

echo character_used>%temp%\trial.txt

for /f %%^" in (%temp%\trial.txt) do (
echo %%^" (double quote^) is chr dec 34)

del %temp%\trial.txt

Quote from: T.C. on January 12, 2010, 02:10:15 PM
Did you try Escaping the chars?

Escaping brings the ones that don't work down to these: 37 ( % ) 38 ( & ) 44 ( , ) 59 ( ; ) 61 ( = )


Discussion

No Comment Found