I have a batch file that moves some things around and organizes them. I want to add to the end of that batch some code to rename all the files to their same name but the date in addition to the end in only numbers. Maybe I'm bad at searching because I swear this has been covered but I just couldn't find it. So to summarize, I need help writing code that will convert every file in the folder that does not already have the date at the end, to have the date added to the end. So it needs to check whether there are 8 digits at the end and if not then add the date. I'll post my batch file just in case you need to know what I'm doing
rem #echo off
SETLOCAL enableextensions
Set dat=Date
for %%x in (*.pdf) do (
set "_pdfname=%%x"
call :doAllWork
)
goto :eof
:doAllWork
ECHO start
start %_pdfname%
TIMEOUT /T 2 /NOBREAK
start select.vbs
TIMEOUT /T 1 /NOBREAK
start copy.vbs
TIMEOUT /T 1 /NOBREAK
for /F %%g in ('
wmic OS get LocalDateTime /value^|findstr "="
') do for /F %%G in ("%%g") do set "_%%G"
echo %_LocalDateTime:~0,14%
type NUL > TextFiles\%_LocalDateTime:~0,14%.txt
start TextFiles\%_LocalDateTime:~0,14%.txt
TIMEOUT /T 2 /NOBREAK
ECHO close PDF
start close.vbs
TIMEOUT /T 2 /NOBREAK
start window.vbs
TIMEOUT /T 1 /NOBREAK
start paste.vbs
TIMEOUT /T 1 /NOBREAK
start save.vbs
TIMEOUT /T 1 /NOBREAK
start close.vbs
start enter.vbs
move /-y "%_pdfname%" "OldTimesheets\"
TIMEOUT /T 1 /NOBREAK
ECHO exit loop
if exist *.pdf (
goto :eof
) else (
goto :end
)
:end
cscript MessageBox.vbs "This will be shown in a popup."
So at the End all of the new PDFs in OldTimesheets need to be renamed but the old ones will have the date they were put in there.
Thanks ahead of time! This Community is always great!
#echo off
SETLOCAL enableDelayedExpansion
for /f "delims=" %%x in ('dir /b *.pdf') do (
call :doAllWork "%%x"
)
for /f "delims=" %%x in ('dir /b *.pdf') do (
echo %%x | findstr /r "_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\." >nul
if errorlevel 1 ren "%%x" "%%~fx_!_LocalDateTime:~0,8!%%~nx"
)
cscript MessageBox.vbs "This will be shown in a popup."
pause
exit
:doAllWork
ECHO start
start %1
TIMEOUT /T 2 /NOBREAK & start select.vbs
TIMEOUT /T 1 /NOBREAK & start copy.vbs
TIMEOUT /T 1 /NOBREAK
for /F "delims== tokens=2" %%g in ('
wmic OS get LocalDateTime /value ^| find "="
') do set _LocalDateTime=%%g
echo !_LocalDateTime:~0,14!
type NUL > TextFiles\!_LocalDateTime:~0,14!.txt
start TextFiles\!_LocalDateTime:~0,14!.txt
TIMEOUT /T 2 /NOBREAK & ECHO close PDF & start close.vbs
TIMEOUT /T 2 /NOBREAK & start window.vbs
TIMEOUT /T 1 /NOBREAK & start paste.vbs
TIMEOUT /T 1 /NOBREAK & start save.vbs
TIMEOUT /T 1 /NOBREAK & start close.vbs & start enter.vbs
move /-y "%1" "OldTimesheets\"
TIMEOUT /T 1 /NOBREAK & ECHO exit loop
exit /b
Notes:
Replaced direct enumeration of *.pdf with dir /b *.pdf to prevent possible re-processing of files after they have been renamed.
Added SETLOCAL enableDelayedExpansion to make _LocalDateTime change its value at each assignment, also ! instead of % should be used in such case
Removed SETLOCAL enableextensions as it's enabled by default
Simplified a few things to improve readability
I figured it out on my own! I added
set str=%date%
echo.%str%
set str=%str:/=%
echo.%str%
at the begging and used
ren OldTimesheets\%_pdfname% %str%%_pdfname%
right after the move line. Works like a charm! Thanks for the help anyways!
Related
Ive been trying to loop a batch file exactly 5 times using the set /a operation and so far no luck! Could someone help me?
#ECHO OFF
set loopCount=0
:loop
echo Checked %loopCount% times...
set /a loopCount=1+%loopCount%
if loopCount == 5 (goto exit) else (goto loop)
:exit
cls
echo Finished after %loopCount% times
pause >nul
Best of luck to whoever can help me
Here's a couple of examples to help you.
Using GoTo with a label:
#Set "loopCount=0"
:loop
#Set /A loopCount += 1
#Rem Some actual command goes here.
#Echo Checked %loopCount% times...
#If %loopCount% Lss 5 GoTo loop
#%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
#ClS
#Echo Finished after being checked %loopCount% times.
#Pause 1>NUL
Alternatively, using a For /L looping mechanism:
#Set "maxCount=5"
#For /L %%G In (1,1,%maxCount%) Do #(
Rem Some actual command goes here.
Echo Checked %%G times...
)
#%SystemRoot%\System32\timeout.exe /T 2 /NoBreak 1>NUL
#ClS
#Echo Finished after being checked %maxCount% times.
#Pause 1>NUL
If you need to execute the entire batch script 5 times, the following method of storing an execution count in an alternate data stream of the script can be used:
#Echo off
more < "%~f0:Count" 2>&1 > nul && (
For /f "usebackq delims=" %%G in (`more ^< "%~f0:Count"`)Do Set /A Count=%%G+1
) || (
Set /A "Count+=1"
)
Echo(%Count% >"%~f0:Count"
:# Your script below
Echo(Script execution: %Count%
:# Terminate script execution after count reached
If %Count% EQU 5 (
Echo(0 >"%~f0:Count"
Exit /B
)
"%~f0"
I am using this batch to automatically restart some program, they are running very well, but sometimes the batch exit, which causes process_1 and process_2 also exit. It seems that everything is normal, but the batch itself crashes. Why ?
#echo off
cd %~dp0
set process_1=process_1.exe
set process_2=process_2.exe
set interval=10
:check_service
tasklist > task_tmp.txt
findstr %process1% task_tmp.txt> NUL
if ErrorLevel 1 (
timeout /t 1 /nobreak > NUL
goto start_1
)
findstr %process2% task_tmp.txt> NUL
if ErrorLevel 1 (
timeout /t 1 /nobreak > NUL
goto start_2
)
timeout /t %interval% /nobreak > NUL
goto check_service
:start_1
start /b "" %process_1%
echo %date% %time% %process_1% " down, up it" >> start_note.txt
goto check_service
:start_2
start /b "" %process_2%
echo %date% %time% %process_2% " down, up it" >> start_note.txt
goto check_service
You can maybe simplify like this:
#echo off
Set "MyProcess1=process_1.exe"
Set "MyProcess2=process_2.exe"
:check
%SystemRoot%\System32\tasklist.exe /NH | %SystemRoot%\System32\find.exe /i "%MyProcess1%">nul || echo starting %MyProcess1% && start "process 1" "%MyProcess1%"
%SystemRoot%\System32\tasklist.exe /NH | %SystemRoot%\System32\find.exe /i "%MyProcess2%">nul || echo starting %MyProcess2% && start "Process 2" "%MyProcess2%"
%SystemRoot%\System32\timeout.exe /t 10 /nobreak >nul
goto check
Full qualified file names are used for the commands tasklist, find and timeout to make this batch file independent on the values of the local environment variables PATH and PATHEXT and to avoid that the Windows command processor has to search for these three executables.
You can shorten your code like this:
#echo off
pushd "%~dp0"
:chkservice
tasklist | find /I "Process_1.exe" >nul 2>&1
if errorlevel 1 (
start Process_1.exe
echo %date% %time% Process_1.exe Down up, it >>startnote.txt
)
tasklist | find /I "Process_2.exe" >nul 2>&1
if errorlevel 1 (
start Process_2.exe
echo %date% %time% Process_2.exe Down up, it >>startnote.txt
)
timeout /t 10 /nobreak >nul 2>&1
goto chkservice
I have to record a session with a program and want the windows task (1hour) to run a .Bat file to hit for example "F1" to stop the recording then run another .bat file to shutdown the pc.
I dont have problem with turning off pc but the one for "F1".
This is the code i thought it gonna work.
#echo off
cd "C:\Program Files\ShareX"
Start "" /b sharex.exe
timeout /T 5 /nobreak >nul
%SendKeys% "{F1}"
timeout /T 5 /nobreak >nul
taskkill /IM sharex.exe /F
Thanks
So, if I understood this question...
This will create the sendkey.vbs to you and run it in your code...
{ sorry my limited English }
#echo off & setlocal enabledelayedexpansion
set "_temp_vbs=%temp%\_temp_file_4vbs_.vbs"
>"!_temp_vbs!"^
(
echo/ Set WshShell = WScript.CreateObject^("WScript.Shell"^)
echo/ Set objShell = WScript.CreateObject^("WScript.Shell"^)
echo/ Wscript.Sleep 1500
echo/ objShell.AppActivate "sharex.exe"
echo/ Wscript.Sleep 1500
echo/ WshShell.SendKeys "({F1})"
)
cd /d "C:\Program Files\ShareX" & Start "" /b sharex.exe
timeout /t 3600 /nobreak && start "" /w "%Windir%\System32\wScript.exe" //nologo "!_temp_vbs!"
>nul 2>nul ((
tasklist | findstr /lic:"ShareX.exe"
) && (
timeout /t 5 /nobreak >nul & taskkill /f /im "ShareX.exe" & del /q /f "!_temp_vbs!"
) || (
call shotdown_pc.bat & exit /b
)) >nul 2>nul
I have a batch file with this code:
#ECHO off
START "" "C:\Program Files (x86)\Proxifier\Proxifier.exe"
timeout /t 5 >NUL
START "" "E:\Program Files (x86)\Epic Games\Launcher\Portal\Binaries\Win64\EpicGamesLauncher.exe"
timeout /t 65 >NUL
Taskkill /IM "Proxifier.exe" /F
Exit
I want to set an expiration date for this batch file.
For example: The programs wont run on 11/30/2018
How do I set an expiration date on this code?
Try this. Example dates are used in the IF conditions in a format YYYYMMDD (you can set your own values):
#echo off
for /f "tokens=* delims=" %%a in ('wmic os get LocalDateTime /value') do for /f "tokens=* delims=" %%# in ("%%a") do set "%%#"
set "LocalDateTime=%LocalDateTime:~0,8%"
::echo %LocalDateTime%
:: EXPIRATION DATE ::
set "EXP_DATE=20181130"
:::::::::::::::::::::
if %LocalDateTime% GTR %EXP_DATE% (
echo this wont work anymore
exit /b
)
START "" "C:\Program Files (x86)\Proxifier\Proxifier.exe"
timeout /t 5 >NUL
START "" "E:\Program Files (x86)\Epic
Games\Launcher\Portal\Binaries\Win64\EpicGamesLauncher.exe"
timeout /t 65 >NUL
Taskkill /IM "Proxifier.exe" /F
Exit
It is very simple:
#echo off
set "expirationDate=30112018"
call:check_expire
if %var_return% EQU 0 (goto main_code) else (exit /b)
goto main_code
:main_code
START "" "C:\Program Files (x86)\Proxifier\Proxifier.exe"
timeout /t 5 >NUL
START "" "E:\Program Files (x86)\EpicGames\Launcher\Portal\Binaries\Win64\EpicGamesLauncher.exe"
timeout /t 65 >NUL
Taskkill /IM "Proxifier.exe" /F
pause
exit /b 0
:check_expire
for %%a in (%date%) do set dt=%%a
For /f "tokens=1-3 delims=/ " %%a in ('echo %dt%') do (
if "%expirationDate%" LEQ "%%a%%b%%c" (set "var_return=1") else (set "var_return=0")
)
Please check also https://social.technet.microsoft.com/Forums/scriptcenter/en-US/14804d47-5887-4119-a9fd-167d2a14df53/set-a-batch-file-expiration-date?forum=ITCG
Hope this helps!
Ok, after seeing crazy stuff being completed in so little code, I have high hopes this is possible.
Pretty much, I want to use the pause command normally, however, if the user doesn't input anything for a specified duration of time, it automatically continues.
In pseudo code:
(sleep %sleep-time%&Echo Pass)1>0 & pause
I thought at first I could do this using start /b to create a process that echoed input while being paused i the current thread, but that could cause problems if the user does input something.
Bonus
What would be really cool is if the errorlevel would be changed based on whether the user inputted something, or if the pause command timed out.
I suggest using timeout:
timeout /T 60 >NUL
This will sleep your script for 1 minute, or unless the user hits a key.
#echo off
setlocal
rem TimedPause.bat - Antonio Perez Ayala
if "%1" equ ":PausePart" goto PausePart
if "%1" neq "" goto begin
echo TimedPause.bat seconds
echo/
echo Wait for given seconds or until user press a key
echo At end, the presence of keyPressed.txt file indicate the cause of exit
goto :EOF
:begin
set seconds=%1
start "" /B "%~F0" :PausePart
for /F "skip=2 tokens=2 delims=," %%a in ('tasklist /FI "IMAGENAME eq cmd.exe" /FO CSV') do (
set PausePart=%%a
goto TimePart
)
:TimePart
ping -n 2 localhost > NUL
if exist keyPressed.txt goto :EOF
set /A seconds-=1
if %seconds% gtr 0 goto TimePart
taskkill /PID %PausePart% /F > NUL
goto :EOF
:PausePart
del keyPressed.txt 2> NUL
pause
echo %time% > keyPressed.txt
exit