[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

rename files in sequential serie translation from command prompt to vb script

Hello you helped me on another topic with this:

Hello I want to order a serie of files I have  in c:\data

file001.pdf
file002.pdf
file004.pdf
file007.pdf
file009.pdf
file010.pdf

   and so on any number of files.
   This files could be hundred and there are names that doesn't exist in sequence like file003.pdf, file005.pdf, etc. Now in command prompt what script could be to order in sequence all that files and the numbers of the fies doesn't exist will be taken for next files in sequence for example file003.pdf doesn't exist then file004.pdf will take the name of file003.pdf in our example file007.pdf will take the name of file004.pdf, file009.pdf will take the name of file005.pdf and so on until the end of files .pdf only. The final result pf all process will be in other words:

file001.pdf - not touched because is ok in order
file002.pdf - not touched because is ok in order
file004.pdf - renamed to file003.pdf
file007.pdf - renamed to file004.pdf
file009.pdf - renamed to file005.pdf
file010.pdf - renamed to file006.pdf

 The final files ordered in resume:

file001.pdf
file002.pdf
file003.pdf
file004.pdf
file005.pdf
file006.pdf


   And it works perfect with this script:

@echo off
setlocal enabledelayedexpansion
REM *** Folder in which the files are:
set SourceFolder=D:\Temp\FileRename
REM *** File prefix until the numeric index:
set FilePrefix=file
REM *** File extension to search:
set Extension=pdf
REM *** Length of the numeric index:
set IndexLength=3

set ZeroString=0000000000
call :GetStringLength PrefixLength "%FilePrefix%"
set /a CurrentIndex = 1
for /f "delims=" %%a in ('dir /b /a:-d /o:n "%SourceFolder%\%FilePrefix%*.%Extension%"') do (
	set FileName=%%~na
	set FileIndex=!FileName:~%PrefixLength%,%IndexLength%!
	call :GetFormattedIndex FormattedIndex !CurrentIndex! %IndexLength%
	echo Processing %%a; file index is '!FileIndex!', sequential index is '!FormattedIndex!' ...
	if "!FileIndex!"=="!FormattedIndex!" (
		echo ... indices match, no change.
	) else (
		echo ... file index not in sequential order, will rename.
		ECHO ren "%SourceFolder%\%%a" "%FilePrefix%!FormattedIndex!.%Extension%"
	)
	set /a CurrentIndex += 1
)
goto :eof

:GetFormattedIndex <ReturnVariable> <Index> <Length>
set __string=%ZeroString%%~2
set %1=!__string:~-%3!
goto :eof

:GetStringLength <ReturnVariable> <String>
set /a i = 0
set __string=%~2
:StringLengthLoop
if "!__string:~%i%,1!"=="" (
	set %1=%i%
	goto :eof
) else (
	set /a i += 1
	goto :StringLengthLoop
)
goto :eof

Open in new window


   Is there a way to make a translation of code to vb script for use in asp pages in the middle of:

<%
Code here...
%>

Open in new window


   Why because the server block everything that is a .bat and we canĀ“t use or modify the server is not allowed.
   I hope someone can help to translate the code to use in a regular .asp page. Not "ASP.NET" simply ASP only.
   On the above script the only need to change is the "set SourceFolder=D:\Temp\FileRename" and of course the files must be named like I mentioned in the beginning, it means similar names ending in "00x" with extension in .pdf only like this:

file001.pdf
file002.pdf
file004.pdf
file007.pdf
file009.pdf
file010.pdf

Thank you
0
coerrace
Asked:
coerrace
  • 2
  • 2
1 Solution
 
aikimarkCommented:
rename the .bat to .cmd and try to run it
0
 
coerraceAuthor Commented:
We need the equivalente in VB script to use in asp not run externally with a file. No external files are allowed.
Thank you
0
 
Robberbaron (robr)Commented:
this works for my testing.... change the paths etc to suit

likely to be more efficient methods but speed not likely to be an issue.
option explicit

dim SourceFolder, FilePrefix, Extension, IndexLength

'<<<<<< CONFIG  <<<<<<<<
'*** Folder in which the files are:
SourceFolder = "C:\EE\dert\"
'*** File prefix until the numeric index:
FilePrefix = "test"
'*** File extension to search:
Extension=".trt"
' *** Length of the numeric index:
IndexLength=3
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Dim objFSO, nextIdx, origName, ZeroString, NextName, objFile, iCnt
Set objFSO = CreateObject("Scripting.FileSystemObject")

ZeroString = String(IndexLength,"0")
nextIdx = 1
For iCnt=1 To 999
  origname = SourceFolder & FilePrefix & right(ZeroString & iCnt,IndexLength) & Extension

  If objFSO.FileExists(origname) Then
      nextname = SourceFolder & FilePrefix & right(ZeroString & nextIdx,IndexLength) & Extension
	  'wscript.Echo("move >" & origname & ">to>" & NextName)
	  Set objFile = objFSO.GetFile(origName)
	  objFile.Move(NextName)
	  
	  nextIdx = nextIdx +1   'next item
  End If
Next

Open in new window

0
 
Robberbaron (robr)Commented:
'<<<<<< CONFIG  <<<<<<<<
'*** Folder in which the files are:
SourceFolder = "D:\Temp\FileRename\"
'*** File prefix until the numeric index:
FilePrefix = "file"
'*** File extension to search:
Extension=".pdf"
' *** Length of the numeric index:
IndexLength=3
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Open in new window


note the backslashes at end of path and dot before the extension
0
 
coerraceAuthor Commented:
Thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now