Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi There,
I am trying to figure out how to write a batch program that will take the first 4 characters of a file name (from the cd rom) and move it to the appropriate file name folder. For example, the cd has pdf files called 0001a.pdf, 0002a.pdf, 0003a.pdf, etc. I want to parse the first 4 characters, and move the pdf file to the folders called 0001, 0002, 0003, etc.
Any ideas ?

Untested batch:
GoTo start
Steps to use:
1. Save this as whatever.bat
2. Move whatever.bat to where you want to plant the new directory tree
3. Drag'n'drop the drive/folder icon that holds the all of the files to whatever.bat's icon.
4. I can't be held responsible if you don't follow step 3. May your God have mercy on your computer if you fail to follow step 3.
:start
FOR %%a IN (%1\*.*) DO call :doWork "%%~Na" "%%~Fa"
GoTo :eof:doWork
SET d=%~1
SET d=%d:~0,4%
MD %d%
COPY %2 %d%

a vbscript:
[code]
Option Explicit
Dim objFSO,srcRoot,objFolder,File,dstRoot,theName,dst,dstFile
Set objFSO=CreateObject("Scripting.FileSystemObject")
srcRoot="c:\temp"
dstRoot="c:\test1"
Set objFolder=objFSO.GetFolder(srcRoot)
For Each File In objFolder.Files
If objFSO.GetExtensionName(File) = "pdf" Then
theName= Left(File.Name,4)
dst=dstRoot & "\" & theName
dstFile=dst & "\" & File.Name
If Not objFSO.FolderExists(dst) Then
objFSO.CreateFolder(dst)
End If
objFSO.MoveFile File, dstFile
End If
Next
[/code]

Is it possible to modify this vbs script to just move files to a folder of the same name that already exists? I would also like to rename the once they are moved to the new folder. They would all have the same name.

![]() |
batch search f. and go to...
|
Joining line using awk
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |