Question

I am trying to create a windows script that should generate this kind of filename everytime I run it: filename1, filename2, filename3 and so on. Here is what I have so far:

(
@echo off
wmic logicaldisk get size,freespace,caption
) > disk.txt

I hope you can help me. Thanks!!

No correct solution

OTHER TIPS

:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")

You now have any number of filenames available.

%tempfile%a exists and is empty, but %tempfile%anythingelse should be available for use.


@ECHO OFF
SETLOCAL
SET "basename=filename"
SET /a outname=0
:genloop
SET /a outname+=1
IF EXIST "%basename% %outname%.txt" GOTO genloop
SET "outname=%basename% %outname%.txt"

ECHO %outname%

GOTO :EOF

Ah - increment the destination filename on each run. This should do that. It's not actually creating a file - you'd need to create the file %outname% each time to have it increment...

(the space between %basename% and %outname% is optional, of course - omit it if desired.)


edited to include .txt

This will give you up to 1000 filenames but you can go higher, up to 2 Billion, but the higher you go the longer the delay will be before it picks a filename.

@echo off
for /L %%a in (1,1,1000) do if not defined filename if not exist "filename%%a.txt" set "filename=filename%%a.txt"
(
   wmic logicaldisk get size,freespace,caption
) > "%filename%"
@echo off

    setlocal enableextensions

    call :getNextFilename "filename*.txt" nextFilename
    echo %nextFilename% 
    echo test > "%nextFilename%"

    call :getNextFilename "%cd%\filename*.txt" nextFilename
    echo %nextFilename% 
    echo test > "%nextFilename%"

    endlocal
    exit /b

:getNextFilename whatToSearch returnVariable
    setlocal enableextensions enabledelayedexpansion
    for /f %%a in ("$\%~1"
    ) do for /f "tokens=1,* delims=*" %%b in ("%%~nxa"
    ) do ( set "left=%%b" & set "right=%%c" )
    set "max=0" 
    for %%a in ("%~1"
    ) do for /f "tokens=1 delims=%left%%right% " %%b in ("%%~nxa"
    ) do for /f "tokens=* delims=0 " %%c in ("0%%~b" 
    ) do if %%~c geq !max! set /a "max=%%c+1"
    endlocal & set "%~2=%~dp1%left%%max%%right%" & exit /b

This should find the next file in sequence independently of the existence of holes in the numeration of the files. A path can be included or omitted. The * will be used as the placeholder for the numeration. BUT this will not work if files or included paths have "problematic" characters.

If the date/time of creation of the file can be considered, then this version can be optimized as

:getNextFilename whatToSearch returnVariable
    setlocal enableextensions disabledelayedexpansion
    for /f %%a in ("$\%~1"
    ) do for /f "tokens=1,* delims=*?" %%b in ("%%~nxa"
    ) do ( set "left=%%b" & set "right=%%c" )
    set "max=0" 
    for /f "delims=" %%a in ('dir /tc /o-d /b "%~1" 2^>nul'
    ) do for /f "tokens=1 delims=%left%%right% " %%b in ("%%~nxa"
    ) do for /f "tokens=* delims=0 " %%c in ("0%%~b" 
    ) do set /a "max=%%c+1" & goto done
    :done
    endlocal & set "%~2=%~dp1%left%%max%%right%" & exit /b

that will take the latest created instance of the file set.

I finally figured out where to put the .txt extension. This is from @Magoo's answer but I wanted the file to be a text file so I placed the .txt twice in order for it to work properly.

@ECHO OFF
SETLOCAL
SET "basename=DISK-OUT"
SET /a outname=0

:genloop
SET /a outname+=1

IF EXIST "%basename% %outname%.txt" GOTO genloop
SET "outname=%basename% %outname%.txt"
(
  wmic logicaldisk get size,freespace,caption
) > "%outname%"

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