Question

All, I am trying to rename a file with a time stamp and move it to another folder, this needs to be done on an ongoing basis every second, since a process creates files with the same name, however it will not overwrite until the previous file has been moved.

   :top
    @ECHO OFF
    for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
    set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
    set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

    set "fullstamp=%YYYY%%MM%%DD%.%HH%%Min%%Sec%"

    MOVE /Y C:\MyPath\Scripts\origfile.txt C:\MyPath\Scripts\997\newfile_%fullstamp%.txt

    sleep 1
    goto top

The batch file does its job and renames and moves the file to the new destination. However, the only way I've been able to do this is if I run the batch file and mantain the command window open. I've tried a lot of suggestions, starting the command with the /B switch, launching it from insde a java class, adding exit 0 to the end of the batch script, using nssm to create a service, and run it from there. I am at mental roadblock. All I need to do is to be able to lauch this batch script file in the background, once I am done launching the script, close the window and not have any visible windows open..

THanks all for any help!

Was it helpful?

Solution

This is from a previous answer

@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************

    rem Check if started from javascript part of script.
    rem We are checking an environment variable set from javascript part.
    if "%_run_hidden_%"=="true" (
        goto startBatchWork
    )

    rem if not started from javascript, call javascript part to restart batch.
    wscript //E:JScript "%~dpnx0" 
    exit /b

:startBatchWork

    for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
    set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
    set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
    set "fullstamp=%YYYY%%MM%%DD%.%HH%%Min%%Sec%"

    MOVE /Y C:\MyPath\Scripts\origfile.txt C:\MyPath\Scripts\997\newfile_%fullstamp%.txt
    sleep 1
    goto startBatchWork

    rem End of batch area. Ensure batch ends execution before reaching javascript zone
    exit /b

@end
// **** Javascript zone *****************************************************
// Instantiate the needed component to interact with Shell
var shell = WScript.CreateObject('WScript.Shell');

    // Set the environment variable that the batch part will check to know
    // it's running hidden
    shell.Environment('Process').Item('_run_hidden_')='true';

    // start the batch part of the script calling %comspec% with the current
    // script as parameter, hidden (0) and not waiting for it to end (false)
    shell.Run('"' + shell.ExpandEnvironmentStrings('%comspec%') + '" /c "' + WScript.ScriptFullName + '"', 0, false );

    // All done. Exit
    WScript.Quit(0);

It is a hybrid cmd/javascript file with your code included after the :startBatchWork . Save it as .cmd file.

You can call it as a cmd file. It will start a console (if not in a console) to run the initial batch part. It will check for the presence of a flag variable to determine if the file is running hidden (started from the js part) or not. If it is not running hidden, the javascript part of the code is called and it sets the flag variable and starts the batch file hidden.

Alternatively, you can directly call the javascript part with wscript //e:JScript batchfile.cmd and launch all the process without a initial visible console.

In any case the process will continue running hidden without visible console.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top