Rahmad Handayani
Rahmad Handayani

Reputation: 13

How to check if a file with date name exists from inside a batch file and create file

i got task to make batch file that can detect a file that specified with date in it name. if there is a file exist with that name, the batch process to make empty file with that name.

example : to check if file named INT_SK_20170405.txt exists, if it doesnt exist then make new empty file named INT_SK_20170405.txt

please help

Upvotes: 1

Views: 1043

Answers (1)

npocmaka
npocmaka

Reputation: 57282

You can get the date independent of time settings with WMIC.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YYYYMMDD=%dt:~0,8%" 

if not exist "INT_SK_%YYYYMMDD%.txt" break>"INT_SK_%YYYYMMDD%.txt"

with tomorrow's date:

@echo off
for /f "usebackq" %%# in (`"powershell (Get-Date).AddDays(1).ToString('yyyyMMdd')"`) do set "YYYYMMDD=%%#"

if not exist "INT_SK_%YYYYMMDD%.txt" break>"INT_SK_%YYYYMMDD%.txt"

Upvotes: 2

Related Questions