Making Unique Filenames with Batch

By: dzdrummerzd
July 7, 2012

The following program I threw together pretty quickly because I feel like this is a pretty useful but of code that you could implement in many different ways. The program is as follows:

setlocal enabledelayedexpansion

echo.What would you like to name the file? [format = "NAME"_DateTIME]
set /p name=Name: 
goto main

for /f "delims=/ tokens=1-3" %%a in ("%DATE:~4%") do (
for /f "delims=:. tokens=1-4" %%m in ("%TIME: =0%") do (
set filename=%name%-%%c-%%b-%%a-%%m%%n%%o%%p
if exist %filename%.txt echo.File created successfully!&pause>nul&exit
echo.file was not created successfully!&pause>nul&exit

Now this is just a simple program that outputs one file in the current folder. You could add a lot of stuff like a custom directory, a loop to make it output a bunch of files based on different parameters, different file types other than just simple text documents, you name it. And of course you can switch around how the file name looks to make it suit your needs or to make it more readable.

Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question