| 1. |
Solve : Batch file to move files and rename? |
|
Answer» Hey All, Code: [Select]@echo off copy /v "%%a" d:\tmp\!i!.mod That is a slick way to rename a file but does not weed out duplicate files. It simply gives a new name to the duplicate file. This is fine if only the file name is a duplicate. Also, the for statement does not find .mod files when the path has spaces. The following might work a little better? : ( I believe the "delims=" is necessary. ) C:\>type mod.bat Code: [Select]@echo off setlocal enabledelayedexpansion cd \ set /a j=0 dir /B /s *.mod > mod.txt for /f "delims=" %%a in (mod.txt) do ( set /a j+=1 copy "%%a" E:\tmp\!j!.mod ) C:\>Bill I GAVE your second snippet a try but just got a hung command prompt window. I have also been tinkering with this process is vb script. Anyone think a batch would be better than vbs or vice versa? Thanks, NathanQuote from: nslemmons on October 29, 2009, 05:27:12 PM Bill I gave your second snippet a try but just got a hung command prompt window. I have also been tinkering with this process is vb script. Greg Flowers in post #3 gave the best answer. I merely tried to improve his answer slightly. You will not get a better suggestion than what Greg provided. Good Luck p.s. I started the mod search from root because I don't know where your mod files are stored. You may remove cd \ and start the mod search whereever you please. The mod search from root will find the mod files whereever they are stored on any machine. ( Copy but don't delete any system mod files.) Quote from: nslemmons on October 29, 2009, 05:27:12 PM Bill I gave your code a try but just got a hung command prompt window. Thanks, There is no hang with my code. It worked perfectly. Here is the code and the Output: C:\>type mod.bat Code: [Select]@echo off setlocal enabledelayedexpansion cd \ set /a j=0 dir /B /s *.mod > mod.txt for /f "delims=" %%a in (mod.txt) do ( set /a j+=1 copy "%%a" E:\tmp\!j!.mod ) Output: C:\> mod.bat 1 file(s) copied. 1 file(s) copied. 1 file(s) copied. 1 file(s) copied. 1 file(s) copied. 1 file(s) copied. 1 file(s) copied. 1 file(s) copied. 1 file(s) copied. 1 file(s) copied. 1 file(s) copied. 1 file(s) copied. 1 file(s) copied. C:\>e: E:\>cd tmp E:\tmp>dir Volume in drive E is My Book Volume Serial Number is 0850-D7C5 Directory of E:\tmp 10/28/2009 08:42 PM . 10/28/2009 08:42 PM .. 08/10/1998 05:05 AM 6,089 1.mod 08/10/1998 05:05 AM 35,749 10.mod 08/10/1998 05:05 AM 136,770 11.mod 04/14/2008 07:00 AM 2,080 12.mod 04/14/2008 07:00 AM 2,080 13.mod 08/10/1998 05:05 AM 692 2.mod 08/10/1998 05:05 AM 29,053 3.mod 08/10/1998 05:05 AM 105,878 4.mod 08/10/1998 05:05 AM 1,080 5.mod 08/10/1998 05:05 AM 35,749 6.mod 08/10/1998 05:05 AM 136,770 7.mod 08/10/1998 05:05 AM 6,089 8.mod 08/10/1998 05:05 AM 1,080 9.mod 13 File(s) 499,159 bytes 2 Dir(s) 139,471,556,608 bytes free E:\tmp> P.S. : Greg's copy and rename line is an absolute jewel: copy "%%a" E:\tmp\!j!.modQuote from: nslemmons on October 27, 2009, 06:06:02 PM Hey All, if you want to make sure there are no duplicates after you copy, you can give each file a unique random number, if allowed by your specs. Here's a vbscript you can use Code: [Select]Set objFS=CreateObject("Scripting.FileSystemObject") Randomize strFolder = "c:\test" strDestFolder = "c:\tmp\" Set objFolder = objFS.GetFolder(strFolder) Go (objFolder) Sub Go(objDIR) If objDIR <> "\System Volume Information" Then For Each eFolder in objDIR.SubFolders Go eFolder Next End If For Each strFile In objDIR.Files If objFS.GetExtensionName(strFile) = "mod" Then name =strFile.Name src = strFile.Path rand=Int(1000000 * Rnd ) strName = objFS.GetBaseName(name)&rand objFS.CopyFile src, strDestFolder&strName&".mod" End If Next End Sub save as test.vbs on command line Code: [Select]c:\test> cscript /nologo test.vbs Quote from: nslemmons on October 29, 2009, 05:27:12 PM
I gave the vbs code by Ghost in post #9 a run and got no output the first time. And got "permission denied" the second time. Please post the VBS output here when you run the vbs code by Ghost in post #9. Thanks Quote from: billrich Please post the VBS output here when you run the vbs code by Ghost in post #9. since you insists.. Code: [Select]Set objFS=CreateObject("Scripting.FileSystemObject") Randomize strFolder = "c:\test" strDestFolder = "c:\tmp\" Set objFolder = objFS.GetFolder(strFolder) Go (objFolder) Sub Go(objDIR) If objDIR <> "\System Volume Information" Then For Each eFolder in objDIR.SubFolders Go eFolder Next End If For Each strFile In objDIR.Files If objFS.GetExtensionName(strFile) = "mod" Then name =strFile.Name src = strFile.Path rand=Int(1000000 * Rnd ) strName = objFS.GetBaseName(name)&rand objFS.CopyFile src, strDestFolder&strName&".mod" End If Next End Sub save as test.vbs on command line Code: [Select]C:\test>more test.vbs Set objFS=CreateObject("Scripting.FileSystemObject") Randomize strFolder = "c:\test" strDestFolder = "c:\tmp\" Set objFolder = objFS.GetFolder(strFolder) Go (objFolder) Sub Go(objDIR) If objDIR <> "\System Volume Information" Then For Each eFolder in objDIR.SubFolders Go eFolder Next End If For Each strFile In objDIR.Files If objFS.GetExtensionName(strFile) = "mod" Then name =strFile.Name src = strFile.Path rand=Int(1000000 * Rnd ) strName = objFS.GetBaseName(name)&rand objFS.CopyFile src, strDestFolder&strName&".mod" End If Next End Sub C:\test>dir /B /S C:\test\.txt C:\test\file C:\test\output.txt C:\test\test C:\test\test.awk C:\test\test.bat C:\test\test.vbs C:\test\test1 C:\test\test\aslfaa.mod C:\test\test\sdlfjasf.mod C:\test\test\test.asfklhasl C:\test\test\test1 C:\test\test\test2.doc C:\test\test1\123.mod C:\test\test1\3231.mod C:\test>dir c:\tmp Volume in drive C has no label. Volume Serial Number is 08AC-4F03 Directory of c:\tmp 10/30/2009 11:20 AM <DIR> . 10/30/2009 11:20 AM <DIR> .. 0 File(s) 0 bytes 2 Dir(s) 4,733,227,008 bytes free C:\test>cscript /nologo test.vbs C:\test>dir /B /S c:\tmp c:\tmp\123176457.mod c:\tmp\323114370.mod c:\tmp\aslfaa254512.mod c:\tmp\sdlfjasf277152.mod it works for me. any discrepancies at your side is your own doing.Quote from: gh0std0g74 on October 29, 2009, 09:17:01 PM since you insist. . . |
|