1.

Solve : Batch file to move files and rename?

Answer»

Hey All,

I've got a tricky situation that I need some pro help on.

I have a directory with multiple folders and inside these folders are files with a .MOD extension.

I need to move all the files with the .MOD extension to another folder and rename the files as they are moved (I would prefer to copy in case any thing goes awry). I need to rename as they are copied due to the files having the same name.

I found that I can EASILY move files with the command prompt but with the added variables of renaming and multiple folders I'm not quote sure how to tackle this.

Any help is appreciated.

Thanks,
NathanWhat I would do is instead of SAY copying from point A to C is to add a step in between as a conversion cache to copy the files to this B location then use a wild card to change all extensions to the extension you want. Then have an instruction that will copy these from that B location to the destination you want being C. Then have a cleanup that deletes all data from location B so that the next data to go in is clean vs the prior data file extension conversion.

Maybe someone knows of an easier method, but I believe it will be a 3 location process where you have your origin of your data, a conversion location, and then the destination.Files never have the same name in the current directory. The OS will not allow two files with the same in the same directory. Try to name a file the same name as an existing file name in the same directory and report the error message.This may provide you a starting point. It will copy all files that end in mod extention below the current directory to different directory and rename to a number with mod extension.
Code: [Select]@echo off
setlocal enabledelayedexpansion
set i=0
for /F %%a in ('dir /B /S *.mod') do (
set /a i+=1
copy /v "%%a" d:\tmp\!i!.mod 'Change the path before you run this script
)
@billrich:
(not to argue or anything) i think he means to grab the .MOD files only (not the directories as well), and move them (while renaming). So this way there could be a case where 2 files have the same name in different directories and now they are going into one...Quote from: gregflowers on October 28, 2009, 02:14:38 PM

Code: [Select]@echo off
setlocal enabledelayedexpansion
set i=0
for /F %%a in ('dir /B /S *.mod') do (
set /a i+=1
copy /v "%%a" d:\tmp\!i!.mod
)

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.

Anyone think a batch would be better than vbs or vice versa?


Nathan

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,
Nathan

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,

I've got a tricky situation that I need some pro help on.

I have a directory with multiple folders and inside these folders are files with a .MOD extension.

I need to move all the files with the .MOD extension to another folder and rename the files as they are moved (I would prefer to copy in case any thing goes awry). I need to rename as they are copied due to the files having the same name.

I found that I can easily move files with the command prompt but with the added variables of renaming and multiple folders I'm not quote sure how to tackle this.

Any help is appreciated.

Thanks,
Nathan

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

Anyone think a batch would be better than vbs or vice versa?

Thanks,
Nathan

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. . .


"It works for me. any discrepancies at your side is your own doing."

Bill wrote:

The request on post #10 was for Nathan, the Original Poster to show the Output of the ghost VBS. We don't need more misleading information from the Ghost.

The batch file by Greg is much better.

You were told to send the files to another drive.



Discussion

No Comment Found