Solved

Convert my VB into a Batch File

Posted on 2007-03-23
14
253 Views
Last Modified: 2010-04-16
I got a VBScript that i created in order to change/update filenames in a specified folder based on a couple of IF factors. I was hoping to convert it into a DOS-Batch file for ease of being able to run it on a clients machine which doesn't have Visual Studio, just set a specified path for them then all they would have to do is run the batch any time to update files in the folder.

My VB skills are minimal and Batch Program skills next to none (just a few basic cmds), so I was hoping someone could so kindly translate it into Batch Format, or even open for simple alternative suggestions.


Dim objFSO
        Dim strCurrentFolder
        Dim objFolder
        Dim newFile
        Dim objFile
        objFSO = CreateObject("Scripting.FileSystemObject")
        strCurrentFolder = "I:\Project_Data\"
        objFolder = objFSO.GetFolder(strCurrentFolder)
        For Each objFile In objFolder.Files
            If UCase(Right(objFile.Name, 3)) = "PDF" And InStr(objFile.Name, "ABC") Or InStr(objFile.Name, "EFG") Then
                If Mid(objFile.Name, 14, 1) = "." Then
                    newFile = "2007-" & Left(objFile.Name, 8) & "LID" & Mid(objFile.Name, 8, 6) & ".pdf"
                    objFSO.MoveFile(objFile, strCurrentFolder & newFile)
                End If
            End If
        Next
        For Each objFile In objFolder.Files
            If UCase(Right(objFile.Name, 3)) = "PDF" And InStr(objFile.Name, "JKLM") Or InStr(objFile.Name, "EFGH") Then
                If Mid(objFile.Name, 15, 1) = "." Then
                    newFile = "2007-" & Left(objFile.Name, 9) & "LID" & Mid(objFile.Name, 9, 6) & ".pdf"
                    objFSO.MoveFile(objFile, strCurrentFolder & newFile)
                End If
            End If
        Next



Thanks
Jay
0
Comment
Question by:SES_Team
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 18778720
You don't need to convert this to a batch file. VBScript does not require visual studio installed in order to run. It runs within the Scripting Runtime context and uses either CScript or WScript to run. These are installed usually with internet explorer and / or the operating system itself. If you have a .vbs script file then it can be run on any machine where this is not explicitly removed or disabled. Most machines will have the scripting runtime already installed.

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18778894
Just to complement what TimCottee has correctly stated, if your code is simply saves as a file with the extension .vbs, then you can simply Double-click on that file, and it will run, automatically.

In addition, if you have any version of Windows newer than Windows 98, there is no version of DOS whatsoever on your box.  That has been completely integrated with Windows.

AW
0
 

Author Comment

by:SES_Team
ID: 18779361
Excuse me for I mixed around my terminology. Your right about DOS, I was thinking Windows Batch (cmd prmpt) but said DOS-Batch. Also I said that I had VBScript when I actually have VB macro.

I appreciate the idea though, of course seems easier then coming up with a batch file. Can you please give me a little hint what I'm missing. I'm getting a compilation error - "Cannot use parentheses when calling a Sub".

What do I need to change?

Jay

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18779383
I believe it's these lines...
objFSO.MoveFile(objFile, strCurrentFolder & newFile)
objFSO.MoveFile(objFile, strCurrentFolder & newFile)

remove the parentheses and it should work...
0
 

Author Comment

by:SES_Team
ID: 18779532
I tried that after receiving the error msg and tracing down the line & char mentioned from the error. When I remove the parentheses from those lines I don't receive an error but then the MoveFile cmd doesn't work and nothing updates as it should.

It works when I run it in MS Visual Studio, just when trying to use it as vbs has some issue.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18779563
Let's try this...missing a couple of 'set's above...

Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
strCurrentFolder = "I:\Project_Data\"
Dim objFolder:objFolder = objFSO.GetFolder(strCurrentFolder)
Dim newFile
Dim objFile
For Each objFile In objFolder.Files
  If UCase(Right(objFile.Name, 3)) = "PDF" And InStr(objFile.Name, "ABC") Or InStr(objFile.Name, "EFG") Then
    If Mid(objFile.Name, 14, 1) = "." Then
    newFile = "2007-" & Left(objFile.Name, 8) & "LID" & Mid(objFile.Name, 8, 6) & ".pdf"
    objFSO.MoveFile(objFile, strCurrentFolder & newFile)
    End If
  End If
Next
For Each objFile In objFolder.Files
  If UCase(Right(objFile.Name, 3)) = "PDF" And InStr(objFile.Name, "JKLM") Or InStr(objFile.Name, "EFGH") Then
    If Mid(objFile.Name, 15, 1) = "." Then
      newFile = "2007-" & Left(objFile.Name, 9) & "LID" & Mid(objFile.Name, 9, 6) & ".pdf"
      objFSO.MoveFile(objFile, strCurrentFolder & newFile)
    End If
  End If
Next
0
 

Author Comment

by:SES_Team
ID: 18779687
I don't see what is really different besides the format of moving things around. And I get that error again pertaining to;

objFSO.MoveFile(objFile, strCurrentFolder & newFile)

Could it be that I'm calling it as a Sub(), in vbs do I need to call it using Function or something other then Sub()?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 67

Expert Comment

by:sirbounty
ID: 18779699
Try it by specifying objFile.Path instead of objFile

objFSO.MoveFile objFile.Path, strCurrentFolder & newFile
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18779767
Try this. It's in debug mode so it just echos out the command. To activate REM the set debug statement as annotated in the REM statement.

@echo off

setlocal

REM ** REM out the next statement to enable processing: REM set debug=echo
set debug=echo

set curFolder=I:\Project_Data
set fileMask=*.pdf

pushd "%curFolder%"

for /f "tokens=*" %%a in ('dir /b /a-d "%fileMask%" 2^>NUL ^| findstr /i "ABC EFG"') do call :PROCESS "%%a"

popd

goto :EOF

:PROCESS

set fileName=%~1

if not "%fileName:~14,1%"=="." goto :EOF

set fileName=2007-%fileName:~0,8%LID%fileName:~8,6%.pdf

%debug% ren "%~1" "%fileName%"

Good Luck,
Steve
0
 

Author Comment

by:SES_Team
ID: 18821971
Sirbounty,

Thanks for the help, but I receive the same error when trying to specify objFile.Path instead of objFile.

Line:    16
Char:   73
Error:  Cannot use parentheses when callin a s Sub
Code:  800A0414

0
 

Author Comment

by:SES_Team
ID: 18821983
Steve,

I have also tried your batch, while turning off echo it will run but doesn't seem to do anything. I don't recieve any msg, it runs then closes and nothing is update.

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18822595
Line 16 appears to be

 If Mid(objFile.Name, 15, 1) = "." Then

What are you trying to accomplish there?  I'd suspect it's processing a file that's not 15 characters long...

I think you're better off using
if right(objfile.name,4"="."
or if just getting the extension
if objfso.getextensionname(file.name)="pdf"
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 125 total points
ID: 18822772
Let me place some debugging information in and have you paste the output:

@echo off

setlocal

REM ** REM out the next statement to enable processing: REM set debug=echo
set debug=echo

set curFolder=I:\Project_Data
set fileMask=*.pdf

pushd "%curFolder%"

for /f "tokens=*" %%a in ('dir /b /a-d "%fileMask%" 2^>NUL ^| findstr /i "ABC EFG"') do call :PROCESS "%%a"

popd

goto :EOF

:PROCESS

echo File name: %~1

set fileName=%~1

if not "%fileName:~14,1%"=="." goto :EOF

set fileName=2007-%fileName:~0,8%LID%fileName:~8,6%.pdf

%debug% ren "%~1" "%fileName%"
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18842762
Just love the grading system here... Makes perfect sense --- lol
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stringclean challenge 26 56
Vbscript 8 65
control image tags in a string ? 12 61
Windows batch files 3 24
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about a problem I had getting the GPS LocationListener working.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now