Quick bat file help

Don
Joined
21 Oct 2002
Posts
46,828
Location
Parts Unknown
say a bat file is creating file.txt

i want to put something in, so if file.txt exists, when it comes to save, it creates filea.txt

if file a exists, then create fileb.txt

-ie, instead of overwriting or appending the current files, just adding a new letter to the end of it

this won't happen more than 10 times a day, so i won't run out of the alphabet..


can anyone help :)


i know it's something like..

file%%i.txt if %%i exists %%i = (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,w,y,z)



just not sure how to write it
 
DO a for loop to find the last saved file...

have a text file called alphabet with a-z one letter on each line. you could continue with aa ab etc

Code:
SETLOCAL enabledelayedexpansion

for /f %%a in (alphabet.txt) DO (
    if NOT exist !file!%%a.txt (
           set postfix=%%a
           goto :nextletterfound
  ) ELSE (
     echo !file!%%a.txt Exists! Checking next letter...
       IF "%%a"=="z" (
          echo Problem... letter Z is reached and !file!%%a.txt Exists!
          echo terminiating 
          GOTO : error
        )
   )
)
:error 
echo errorroror
blah bla

:nextletterfound
echo next letter is %postfix%

etc etc


just of the top of my head, use as you like, maybe be wrong, yadda yada
 
Last edited:
Kronologic

i've got the file doing this..

@echo off
title close my documents..
color c7
echo Close all My Documents..
@echo off
pause
color 17

::date code

FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO (call :s_fixdate %%G %%H %%I %%J)
goto :s_print_the_date

:s_fixdate
if "%1:~0,1%" GTR "9" shift
FOR /f "skip=1 tokens=2-4 delims=(-)" %%G IN ('echo.^|date') DO (
set %%G=%1&set %%H=%2&set %%I=%3)
goto :eof

:s_print_the_date

ENDLOCAL&SET mm=%mm%&SET dd=%dd%&SET yy=%yy%
::date code

title 7zip backup..
echo

7za.exe a -mx=9 E:\Bleddyn\backups\%dd%-%mm%-%yy%.7z "%userprofile%\My Documents\" -x!"My Documents\tempSimsRpt\" -x!"My Documents\My SIMS Documents\"
@echo off
color a0

title all done
echo all done
@echo off
pause

which names the backups.. 28-01-2008.7z , i'm hoping to add the letters so i can run this more than once a day without having to manually edit the filename..

whitecrook, that looks like what i need, but i'm a bit of a bat file noobie.. i'd like to keep it all in one file if poss, thanks for the info :)
 
OK Bledd I wrote you a whole thing that works for ya:


You can test it simply by running it from the command prompt like
Code:
 bledd.bat c:\tmp\
and it will save a DIRECTORY LISTING of C:\ to the C:\tmp folder (make one if you don't have one already or change the script. - didn' seem to work for the root of the drive i.e. c:\ ) drive using the prefix and extension supplied in the script. keep running and it will incrememnt the counter until the last value is reached.


Its fairly self explanitory.

I also included a fixSLASH function I wrote which will remove/add a trailing slash from a dir path supplied unless there is already one there (or not...)

Regards,


edit - There was a couple of errors with spaces and stuff. sorted. Now works properly :)

Code:
@ECHO OFF

:MAINSAMPLEPROG
SETLOCAL
CALL :findnextfilename testpref txt %1
IF %RETURN% EQU 0 (
		DIR c:\ > %RETURNEDPATH%\%RETURNEDFILENAME%
	) ELSE (
		ECHO ERROR TERMINATING - FINDNEXTNAME MUST HAVE RETURNED UNSUCCESSFUL.
		GOTO :EOF
	)


	
	
	
ECHO SUCCESS FILE SAVED AS %RETURNEDPATH%\%RETURNEDFILENAME%
ECHO END.
ENDLOCAL
GOTO :EOF






REM -------------------------------------------------------------







:findnextfilename
SETLOCAL
REM - %1 = FILE PREFIX
REM - %2 = FILE EXTENSION
REM - %3 = PATH TO SEARCH FOR PREVIOUSLY NAMED DILES

SET FILEPREFIX=%1
SET EXT=%2
SET PATHTOSEARCH=%3

REM - NEXT LINE POSIBLE POSTFIXES IN ORDER DELIMETED BY A SPACE
SET POSTFIXES=a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae


REM - - edit setting above this line - - - - - -



REM remove trailing slash from pathtosearch if it has one, else leave it as is
CALL :fixSLASH PATHTOSEARCH REMOVE %PATHTOSEARCH%


FOR %%A IN (%POSTFIXES%) DO SET LASTPOSTFIX=%%A
ECHO %PATHTOSEARCH%

FOR %%A in (%POSTFIXES%) DO (
	IF NOT EXIST "%PATHTOSEARCH%\%FILEPREFIX%%%A.%EXT%" SET POSTFIX=%%A& GOTO :nextletterfound

	IF EXIST "%PATHTOSEARCH%\%FILEPREFIX%%%A.%EXT%" ECHO %FILEPREFIX%%%A.%EXT% Exists! Checking next letter...

	IF "%%A"=="%LASTPOSTFIX%" (
		ECHO Problem... last postfix "%LASTPOSTFIX%" is reached and %FILEPREFIX%%LASTPOSTFIX%.%EXT% Exists!
		ECHO Terminating with value RETURN=1
		GOTO :error
    )
)

:error 
(ENDLOCAL & REM
	ECHO Error.
	SET RETURN=1
)
GOTO :EOF

:nextletterfound
(ENDLOCAL & REM
	ECHO Postfix chosen is "%POSTFIX%"
	ECHO Complete filename is %PATHTOSEARCH%\%FILEPREFIX%%POSTFIX%.%EXT% 
	SET RETURNEDPATH=%PATHTOSEARCH%
	SET RETURNEDFILENAME=%FILEPREFIX%%POSTFIX%.%EXT%
	SET RETURN=0
	)
GOTO :EOF






REM -------------------------------------------------------------



@ECHO OFF
:fixSLASH

REM fixSLASH.bat
REM Tool to add a trailing slash to a path only if the path doesn't have one already and vice-versa.
REM %1 IS VARIBALE TO RETURN PATH IN
REM %2 IS ADD or REMOVE - required out put
REM %3 IS the ACTUAL PATH

SETLOCAL
SET VER=1.0
SET TMP1=%3
SET VARIABLE=%TMP1%
IF "%3"=="" GOTO :failedExit
SET DIDIT=No action was required.

IF "%2%"=="REMOVE" (
	IF "%TMP1:~-1%"=="\" SET VARIABLE=%TMP1:~0,-1%& SET DIDIT=Slash was removed.
)

IF "%2"=="ADD" (
	IF NOT "%TMP1:~-1%"=="\" SET VARIABLE=%TMP1%\& SET DIDIT=Slash was added.
)

(ENDLOCAL & REM
	SET RETURN=0
	SET %1=%VARIABLE%
	ECHO fixSLASH: Path %3 requested trailing slash %2.
	ECHO fixSLASH: %DIDIT%
	ECHO fixSLASH: Result: %VARIABLE%
)
GOTO :EOF

:failedExit
(ENDLOCAL & REM
	ECHO fixSLASH: Not all params supplied. Exiting fixSLASH.bat
	SET RETURN=1
)
GOTO :EOF
 
Last edited:
i ended up just chucking this in, i'm not good enough at bat files to get my head around what you wrote :/ tried using it with my file, but couldn't get it working

so this is what i'm using now, looks crap & wastes space in the file, but it's working perfectly

set v=a
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%a.7z set v=b
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%b.7z set v=c
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%c.7z set v=d
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%d.7z set v=e
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%e.7z set v=f
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%f.7z set v=g
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%g.7z set v=h
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%h.7z set v=i
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%i.7z set v=j
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%j.7z set v=k
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%k.7z set v=l
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%l.7z set v=m
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%m.7z set v=n
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%n.7z set v=o
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%o.7z set v=p
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%p.7z set v=q
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%q.7z set v=r
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%r.7z set v=s
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%s.7z set v=t
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%t.7z set v=u
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%u.7z set v=v
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%v.7z set v=w
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%w.7z set v=x
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%x.7z set v=y
if exist E:\Bleddyn\backups\%dd%-%mm%-%yy%y.7z set v=z


::v being the letter for the filename, so i make the file... E:\Bleddyn\backups\%dd%-%mm%-%yy%%v%.7z
 
Kronologic

i've got the file doing this..



which names the backups.. 28-01-2008.7z , i'm hoping to add the letters so i can run this more than once a day without having to manually edit the filename..

whitecrook, that looks like what i need, but i'm a bit of a bat file noobie.. i'd like to keep it all in one file if poss, thanks for the info :)

I would create the timestamp as YYMMDD as it sorts better also if you ad a time in too (ie YYMMDD_HHMM) then you will have no duplication errors. If you want to remove old versions then add an remove where *YYMMDD* = YYMMDD-1
 
i ended up just chucking this in, i'm not good enough at bat files to get my head around what you wrote :/ tried using it with my file, but couldn't get it working


Use it like this:

1. Copy all this into your file:

Code:
@ECHO OFF

:MAINSAMPLEPROG
SETLOCAL

CALL :findnextfilename %dd%-%mm%-%yy% 7z E:\Bleddyn\backups\


IF %RETURN% EQU 0 (
		ECHO RETURNED PATH IS %RETURNEDPATH%
		ECHO RETURNED FILENAME IS %RETURNEDFILENAME%
		ECHO RETURNED POSTFIX IS %POSTFIX%
	) ELSE (
		ECHO ERROR TERMINATING - FINDNEXTNAME MUST HAVE RETURNED UNSUCCESSFUL.
		GOTO :EOF
	)
	

ECHO END.
ENDLOCAL
GOTO :EOF






REM -------------------------------------------------------------







:findnextfilename
SETLOCAL
REM - %1 = FILE PREFIX
REM - %2 = FILE EXTENSION
REM - %3 = PATH TO SEARCH FOR PREVIOUSLY NAMED DILES

SET FILEPREFIX=%1
SET EXT=%2
SET PATHTOSEARCH=%3

REM - NEXT LINE POSIBLE POSTFIXES IN ORDER DELIMETED BY A SPACE
SET POSTFIXES=a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae


REM - - edit setting above this line - - - - - -



REM remove trailing slash from pathtosearch if it has one, else leave it as is
CALL :fixSLASH PATHTOSEARCH REMOVE %PATHTOSEARCH%


FOR %%A IN (%POSTFIXES%) DO SET LASTPOSTFIX=%%A
ECHO %PATHTOSEARCH%

FOR %%A in (%POSTFIXES%) DO (
	IF NOT EXIST "%PATHTOSEARCH%\%FILEPREFIX%%%A.%EXT%" SET POSTFIX=%%A& GOTO :nextletterfound

	IF EXIST "%PATHTOSEARCH%\%FILEPREFIX%%%A.%EXT%" ECHO %FILEPREFIX%%%A.%EXT% Exists! Checking next letter...

	IF "%%A"=="%LASTPOSTFIX%" (
		ECHO Problem... last postfix "%LASTPOSTFIX%" is reached and %FILEPREFIX%%LASTPOSTFIX%.%EXT% Exists!
		ECHO Terminating with value RETURN=1
		GOTO :error
    )
)

:error 
(ENDLOCAL & REM
	ECHO Error.
	SET RETURN=1
)
GOTO :EOF

:nextletterfound
(ENDLOCAL & REM
	ECHO Postfix chosen is "%POSTFIX%"
	ECHO Complete filename is %PATHTOSEARCH%\%FILEPREFIX%%POSTFIX%.%EXT% 
	SET RETURNEDPATH=%PATHTOSEARCH%
	SET RETURNEDFILENAME=%FILEPREFIX%%POSTFIX%.%EXT%
	SET RETURN=0
	)
GOTO :EOF






REM -------------------------------------------------------------



@ECHO OFF
:fixSLASH

REM fixSLASH.bat
REM Tool to add a trailing slash to a path only if the path doesn't have one already and vice-versa.
REM %1 IS VARIBALE TO RETURN PATH IN
REM %2 IS ADD or REMOVE - required out put
REM %3 IS the ACTUAL PATH

SETLOCAL
SET VER=1.0
SET TMP1=%3
SET VARIABLE=%TMP1%
IF "%3"=="" GOTO :failedExit
SET DIDIT=No action was required.

IF "%2%"=="REMOVE" (
	IF "%TMP1:~-1%"=="\" SET VARIABLE=%TMP1:~0,-1%& SET DIDIT=Slash was removed.
)

IF "%2"=="ADD" (
	IF NOT "%TMP1:~-1%"=="\" SET VARIABLE=%TMP1%\& SET DIDIT=Slash was added.
)

(ENDLOCAL & REM
	SET RETURN=0
	SET %1=%VARIABLE%
	ECHO fixSLASH: Path %3 requested trailing slash %2.
	ECHO fixSLASH: %DIDIT%
	ECHO fixSLASH: Result: %VARIABLE%
)
GOTO :EOF

:failedExit
(ENDLOCAL & REM
	ECHO fixSLASH: Not all params supplied. Exiting fixSLASH.bat
	SET RETURN=1
)
GOTO :EOF

or 2. Copy everything after and including findnextfilename into a newfile call it findnextfilename.bat

In your main prog have this command:

Code:
CALL findnextfilename.bat %dd%-%mm%-%yy% 7z E:\Bleddyn\backups\

and it'll work the same, keeping your main batch tidy.
 
Last edited:
Hi, im looking at doing something similar but need some help editing this file. What I need to do is this (ill try and write it down as easily as possible)

Make Dir H:\Pictures\<todays_date> (Where H: is a mapped network drive)

Move I:\DCIM\*.jpg H:\Pictures\<todays_date> (Where I: is a Camera Memory Card)

as simple as it looks i'm finding it incredibly difficult. The two drive letters will never change and have no problem with that. The first obstacle is the mapped drive, I know it wont work if the drive is stated as disconnected under 'My Computer', secondly is the date, it has to be <todays_date> as a variable but simply using %DATE% doesnt work obviously due to the '/' date seperator. I have seen ways of changing this seperator but have been unsuccessful so far.

Any help is great appreciated!
 
bluestreak

a good way of refreshening the map is to unmap, then remap it..

@Echo off
net use h: /d
net use h: \\pcname\sharename
if not exist h:\ goto error
set CURRDATE=%DATE:/=-%
mkdir "H:\Pictures %currdate%"
xcopy "I:\DCIM\*.jpg" "H:\Pictures %currdate%\"
del I:\DCIM\*.jpg /Q
echo All done, no problems
goto end

:error
echo h: not found

:end
pause




should work

thanks for the date tip rpstewart, the one i was using works internationally i think, but i only need a uk one :)
 
Last edited:
Back
Top Bottom