Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi All,
I need some help with a batch file. I have a scanner that once the scan is finished it will prompt the user to provide a name for the scan. Once the name is entered it is programmed to save the scan in a specified directory. I would then like the file to be immediately and automatically appended with the current date and time. Here's an example:Initial file: wo5000.pdf
after rename:
[original filename]_mm_dd_yyyy_hr_min_sec.pdf
w05000_06_10_2009_3_30_25.pdf.After some delay another pdf file with either the same name or a different name will be saved into the directory where it will then be renamed with current date and time. I'm not quite sure what would invoke this process. Any help would be greatly appreciated
Thanks
Jay

Unless I'm missing something isn't this it?
[original filename]_mm_dd_yyyy_hr_min_sec.pdf
w05000_06_10_2009_3_30_25.pdf.

If it doesn't violate locale policy, it will be easier to sort
yyyy_mm_dd_hh_mm_ss
This might provide useful variables in a date stamp:
SETLOCAL ENABLEEXTENSIONS
FOR /F "tokens=1,2 delims= " %%A in ('ECHO %DATE%') DO (
set FULLDATE=%%B
set DOW=%%A
)if "%FULLDATE%"=="" (
goto GB
) else (
goto US
):US
For /f "tokens=1-7 delims=:/-, " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do (
For /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do (
set dow=%%i
set %%a=%%j
set %%b=%%k
set %%c=%%l
set hh=%%m
set min=%%n
set ss=%%o
)
)goto end
:GB
For /f "tokens=1-6 delims=:/-, " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do (
For /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do (
set %%a=%%i
set %%b=%%j
set %%c=%%k
set hh=%%l
set min=%%m
set ss=%%n
)
)set YYMMDD=%yy%%mm%%dd%
set TS=%YYMMDD%%hh%%min%%ss%

David,
Thankyou for the reply. The date format is not critical, I can live with yyyy_mm_dd etc., the date and time stamp is strictly for search purposes.I want to clarify the problem though. The scanner can be configured to either prompt for a name or apply a generic name with a date stamp - not both. By configuring it to prompt for a name, I can have the user insert a name that can be used as another seach parameter. Once it is saved in the specified folder, I want the pdf to be renamed automatically with a date and time stamp. I don't mean to be redundant, but whenever a pdf gets dumped into that folder it will automatically be renamed.
Jay

@Jay Unless I'm missing something isn't this it?
[original filename]_mm_dd_yyyy_hr_min_sec.pdf
w05000_06_10_2009_3_30_25.pdf.No, that's the format of your filenames.
Try the following. You have to supply the info in the command lines marked with <-----. The script is untested. If it checks out aok remove the Echo from the penultimate code line to rename a file immediately after it is written. It's assumed that you enter both filename and extension.
:: Code begins.... @echo off cls setlocal set newfile=%temp%\date.vbs ( echo otherdate = (Date(^)^) echo yy = datePart("yyyy", otherdate^) echo mm = datePart("m" , otherdate^) echo dd = datePart("d" , otherdate^) echo wscript.echo yy^&" "^&mm^&" "^&dd )>>%newfile% FOR /F "tokens=1-3" %%A in ('cscript //nologo %newfile%') do ( set year=%%A set month=%%B set day=%%C ) del %newfile% if %month% lss 10 set month=0%month% if %day% lss 10 set day=0%day% set newdate=_%month%_%day%_%year% set newtime=%time:~0,-3% set newtime=_%newtime::=_% set/p file=Enter filename.extn: echo %file% | scanning_program.exe <------------------------------ pushd specified_directory <----------------------------------------------- for /f "tokens=1-2 delims=." %%1 in ("%file%") do ( set name=%%1 set extn=%%2 ) echo ren "%file%" "%name%%newdate%%newtime%.%extn%" popd :: Code ends...

Valerie,
Thankyou for your response. Please bear with me as my programming experience involves turbo pascal many years ago. I inserted the information needed in the command lines saved the file as a .bat (I'm assuming this is correct) in a folder where a generic pdf is located and ran it. A DOS window pops up requesting filenam.extn. I went ahead and entered a filename and ext. I then removed the echo and tried it again - nothing happened.I'm not sure what the intent off the DOS window is - after a user scans a document, the scanner converts it to a pdf and then a window pops up prompting for a name - the user enters the name and it is then saved to a pre-specified folder. Its at this point I want the date tag added to the filename without any user input.
Thanks
Jay

Thank you, I believe I understand the process. The purpose of using the "pop-up" asking for the filename/extn is so that the entry can be saved to an environment variable which I named FILE for later use. FILE is then (hopefully) piped to the scanner program in the line:
echo %file% | scanning_program.exe <------------------------------i.e. the content of the environment variable FILE (the filename/extn which were entered) is made available to the scanning program when it wants input.
No further user input is required and, if the script is functioning properly, the file should be renamed silently i.e. no notification is displayed.
Can you check the content of the "specified directory" using DIR at the command prompt or Explorer in Windows and advise if the file was renamed? Unfortunately I cannot fully test the scanning/rename functions of the script.
If you enter ECHO %DATE% at the command prompt then post the outcome we can replace the VBS coding making the script a bit shorter.
V.

ECHO %DATE% yields: Thu 06/25/2009.
I ran the scanner as I would normally would and the file was not renamed. What invokes the bat file to execute? One thing to point out, the scanner mfg provides unsupported software that allows for more options in saving and storage - ie prompting for a name, it is that executable file that I inserted into the command line. Also, do I need to provide a path to its location as well?

The bat file sequence is:
1. Prepare the date and time into the format specified by you.
2. Copy the intended filename into a buffer from a keyboard entry which you supply in the format filename.ext
3. Start the scanning program which will read the intended filename from the buffer instead of the keyboard and write the file to the "specified directory".
4. Prepare the filename for alteration.
5. Rename the file written in 2.
The steps have been numbered in the below script.To invoke the bat script either:
(a) Browse to the script using Explorer then click on the file
-or-
(b) Start Cmd.exe and enter the path/filename.bat at the Command prompt.
My preference would be (b).Run the batch script not the scanning program...but remember that the script has not been tested.
If the batch script is not being run from the folder containing the scanning executable and the path to the scanning program is not part of your %path% environment variable then yes, you will have to enter the path into the script as well.
Amended script below:
:: Code begins... @echo off cls setlocal enabledelayedexpansion :: Step 1. set month=%date:~4,2% set day=%date:~7,2% set year=%date:~-4% set newdate=_!month!_!day!_!year! set newtime=!time:~0,-3! set newtime=_!newtime::=_! :: Step 2. set/p file=Enter filename.ext: :: Step 3. echo %file% | scanning_program.exe pushd specified_directory :: step 4. for /f "tokens=1-2 delims=." %%1 in ("!file!") do ( set name=%%1 set extn=%%2 ) :: Step 5. echo ren "!file!" "!name!!newdate!!newtime!.!extn!" popd endlocal :: Code ends...

If we make a few shakey assumptions about the file time...
==========================
@echo off > ## & setLocal enableDELAYedexpansionfor /f "tokens=* delims= " %%a in ('dir /b ##') do (
set str=%%~Ta
set str=!str:~0,-1!
set str=!str:^:=_!
set str=!str: =_!
set str=!str:-=_!
)for /f "tokens=* delims= " %%a in ('dir /b *.pdf') do (
echo ren %%a %%~Na!str!.pdf
)
=====================================
If at first you don't succeed, you're about average.M2

Valerie,
Your explanation in terms of the steps was great - It cleared up a few things for me. Ok, I tried the script however still nothing happened.
I do see issues regarding steps 2 and 3.
Step 3 may be a problem. I will have to verify with the manufacturer, but I don't believe there is an executable file that will actually start the scanner. Its a sheet fed scanner that is mechanically started by a trip switch when the user inserts a sheet into the scanner.
Going back to step 2, If it turns out that the scanner can be activated using a command line, for various reasons, I can't expect the users to initiate a scan via a command line. In this scenario for instance, there would have to be a GUI on the screen with a start scan command and that would then request a name, and then tell the user to insert sheet into scanner - that's beyond the scope of this thread.I thought there was a way where a file that was saved to a folder could be auto detected and then renamed accordingly.
Let me try and gather some more info from the manufacturer regarding the executable files and then go from there.
Thankyou to everyone who responded
Jay

It might be a huge pain to do with Command Scripts, but it's fairly easy with VBScript and WMI. Also, you'll need to change the dir to the folder in question.
'For dir, replace every backslash (\) with four backslashes (\\\\) Const dir = "c:\\\\whatever" 'First and foremost: Make sure we have a closeable Command Prompt window, 'as there is no other way to stop the script. If Instr(1, WScript.FullName, "WScript", 1) Then _ WScript.Quit CreateObject("WScript.Shell").Run("cscript.exe """ _ & WScript.ScriptFullName & """", 4, True) Dim name, WMI : Set WMI = GetObject("winmgmts:") While True 'Query shamelessly stolen from 'http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr05/hey0404.mspx With WMI.ExecNotificationQuery ( _ "SELECT * FROM __InstanceCreationEvent WITHIN 5 WHERE " _ & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _ & "TargetInstance.GroupComponent= " _ & "'Win32_Directory.Name=""" & dir & """'") With WMI.Get(.NextEvent.TargetInstance.PartComponent) If StrComp(.Name, name, 1) Then name = .Drive & .Path & .FileName & "_" & GetDateTime & "." & .Extension WScript.Echo "Attempting to rename " & .Name & " -> " & name Do Until .Rename(name) = 0 WScript.Echo "Failed; will retry..." WScript.Sleep 1000 Loop End If End With End With Wend Function GetDateTime Dim d : d = Now GetDateTime = DatePart("yyyy", d) & "_" & DatePart("m", d) & "_" & DatePart("d", d) _ & "_" & DatePart("h", d) & "_" & DatePart("n", d) & "_" & DatePart("s", d) End Function

Razor2.3
All I can say is SWEEET! it works perfect! Thankyou so much. At first I was getting an error message on line 18 expecting ')' this is where I inserted the destination folder. I removed the two ampersands and two quotation marks so it looks like this:
& "'Win32_Directory.Name=""d:\\\\file-rename""'")and it worked fine after thatI gotta learn more about this as I can see all kinds of potential applications with this now.
Valerie, David thankyou for your inputJay

![]() |
![]() |
![]() |

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