batch file help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to write a batch file to strip the 4 leading characters from the
name of all files in a directory. I tried using rename but was not having any
luck getting the pattern matching to strip the characters off. Please let me
know if anyone knows a way to do this in a batch file.
 
EricP said:
I am trying to write a batch file to strip the 4 leading
characters from the name of all files in a directory. I tried
using rename but was not having any luck getting the pattern
matching to strip the characters off. Please let me know if
anyone knows a way to do this in a batch file.
Hi

You can use a VBScript (.vbs file) to do this:


'--------------------8<----------------------

' Folder where files are located
sFolderPath = "c:\test"

Set oFSO = CreateObject("Scripting.FileSystemObject")

' Get all file names that are 5 characters or
' more (excluding file extension) into an Array

arFiles = Array()
Set oFolder = oFSO.GetFolder(sFolderPath)
For Each oFile In oFolder.Files
If Len(oFSO.GetBaseName(oFile.Name)) >= 5 Then
iCount = UBound(arFiles) + 1
ReDim Preserve arFiles(iCount)
Set arFiles(iCount) = oFile
End If
Next

For i = 0 To UBound(arFiles)
sFileName = arFiles(i).Name
sBaseName = oFSO.GetBaseName(sFileName)
sFileExtension = ""
If InStr(sFileName, ".") > 0 Then
sFileExtension = "." & oFSO.GetExtensionName(sFileName)
End If
sNewBaseName = LTrim(Mid(sBaseName, 5))
If sNewBaseName <> "" Then
sNewName = sNewBaseName & sFileExtension
On Error Resume Next
arFiles(i).Name = sNewName
If Err.Number <> 0 Then
WScript.Echo "Error when renaming " & sFileName & " to " & sNewName _
& vbCrLf & "Reason: " & Err.Description
End If
Else
WScript.Echo "Not possible to rename: " & sFileName
End If

Next

MsgBox "Done!", vbSystemModal+vbInformation, "Rename files"

'--------------------8<----------------------
 
EricP said:
I am trying to write a batch file to strip the 4 leading characters from
the
name of all files in a directory. I tried using rename but was not having
any
luck getting the pattern matching to strip the characters off. Please let
me
know if anyone knows a way to do this in a batch file.

you could try asking in 'microsoft.public.win2000.cmdprompt.admin' - they're
experts at this sort of thing.
bob
 
I'd use CKRename (a free tool that's great at that sort of thing)
interactively, but that's not batch file automation.

On batch files, I'd create a disposable set of files and try something
like Ren ????*.* *.* to see if that would work... nope.

OK, next is to look up the For statement, especially the NT
enhancements to it - it can do pretty kewl things now. Let's see...

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx

You can use For to parse all files in a dir, or an entire subtree.
That gets you the file( name)s, but I still can't see how to parse the
strings. So I'd Google for a suitable command line utility...

http://www.google.co.za/search?q=command+line+file+rename+utility+download

Lots of tools, but not sure which can be used from batch files.




------------ ----- ---- --- -- - - - -
The most accurate diagnostic instrument
in medicine is the Retrospectoscope
 
cquirke said:
I'd use CKRename (a free tool that's great at that sort of thing)
interactively, but that's not batch file automation.

On batch files, I'd create a disposable set of files and try something
like Ren ????*.* *.* to see if that would work... nope.

OK, next is to look up the For statement, especially the NT
enhancements to it - it can do pretty kewl things now. Let's see...

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx

You can use For to parse all files in a dir, or an entire subtree.
That gets you the file( name)s, but I still can't see how to parse the
strings.

Here's one way:

@echo off
set tst=12345678
:: Display the 5th and 6t character
echo %tst:~4,2%
:: Display all characters from the 5th character
echo %tst:~4%
 
[/QUOTE]
@echo off
set tst=12345678
:: Display the 5th and 6t character
echo %tst:~4,2%
:: Display all characters from the 5th character
echo %tst:~4%

Arsome, dude! Where did you find that? I didn't see it in...

http://www.microsoft.com/resources/.../all/proddocs/en-us/ntcmds_shelloverview.mspx

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx

....etc.? Got any more?


------------------------ ---- --- -- - - - -
Forget http://cquirke.blogspot.com and check out a
better one at http://topicdrift.blogspot.com instead!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top