|   
 
  
  
    | 
       Batch File Stuff  | 
    
        
  |   
  
Links - external 
http://www.merlyn.demon.co.uk/batfiles.htm 
  ASCII text generator (big fonts) 
  
  
Batch File Compiler - http://sourceforge.net/projects/batchcompiler/ 
  
 
  
Simple Tips 
  
  - dir without headers/footers</STRONG> 
 dir C:\directoryname /s 
  /a:-d |find "/"    /s will list subdirectories    
  /a:-d will suppress directory entries 
   - 
  
 - Where.exe (DOS command) - how to use 
  
 -   - search path - where $path:cl*.dll
   
  
FOR command - also the strip characters https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10)?redirectedfrom=MSDN type 
"FOR /?"
  
@ECHO OFF SETLOCAL set file=C:\Users\SOMEUSER\Desktop\fs.cfg FOR %%i 
IN ("%file%") DO ( ECHO filedrive=%%~di ECHO filepath=%%~pi ECHO 
filename=%%~ni ECHO fileextension=%%~xi ECHO filefull=%%~fi ) 
  
-- instead of %%f use %%~nxf (we combined n and x from above 
for /f "delims=" %%f in (%file_list%) do (  robocopy 
%serverupdatepath% %clientpath%\ %%~nxf /NJH /NJS ) 
  
Command-line reference A-Z https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490890(v=technet.10)?redirectedfrom=MSDN 
  
  
  
  
Robocopy Info and Tips 
  
Syntax http://technet.microsoft.com/en-us/library/cc733145%28WS.10%29.aspx 
  
GUI http://technet.microsoft.com/en-us/magazine/2006.11.utilityspotlight.aspx 
  
Wiki http://en.wikipedia.org/wiki/Robocopy 
  
base needs of command line (the retries and wait...if left out you could lock 
up a job for awhile) robocopy %SOURCE% %DESTINATION% /r:0 
/w:1 /r:<N> = Specifies the number of retries on failed copies. The 
default value of N is 1,000,000 (one million retries). /w:<N> = 
Specifies the wait time between retries, in seconds. The default value of N is 
30 (wait time 30 seconds). 
   
  
 Exit the batch file and not close the CMD window 
- "exit /B"
  
My "Start /Wait" command won't 
wait 
Compressed 
  Executables - If you use the batch file "START /WAIT" command or the scripting 
  "wsh.Run,,True" method and expect a program to wait -- but it doesn't wait -- 
  well, the most likely cause is that the program you tried to run was 
  "compressed". What happens is that the program decompresses itself in memory, 
  starts the new decompressed version as a separate process, then terminates 
  itself. The START command and wsh.Run method see the original program 
  termination and assume everything is done! If you open your suspect executable 
  in Notepad, you may see some information about what compression program was 
  used. The most popular compression program is UPX. It's popular because 
  it's free and it's excellent! Luckily, UPX can "decompress" the programs it 
  compressed. For example, Symantec's free FixBlast Blaster virus removal tool is UPX 
  compressed and is 133KB. Both START and wsh.Run can't monitor when FixBlast 
  ends. However, if you use UPX to decompress the FixBlast.exe program, it's 
  size jumps to 544KB and START and wsh.Run suddenly begin working as expected! 
  But don't panic if you can't (or aren't allowed to) decompress a program. I 
  show a few other ways of monitoring programs next... 
    
  ideas to accomplish wait - http://www.ericphelps.com/scripting/samples/index.htm#Run
   
   
  
 
Environment Variables Default Values on 
Microsoft Windows XP vs Vista and UP (2008 and up)  more extensive list - http://www.scriptlogic.com/support/CustomScripts/environmentVariableReference.html 
 other - http://technet.microsoft.com/en-us/library/cc749104%28WS.10%29.aspx 
  display/get client name on Terminal Services 
box %clientname% - will display console on a non-ts session 
  
 
  
  
  
  
  
    |   | 
    Vista | 
    XP |  
  
    | Variable | 
    Typical value (May vary, 
      depending on system) | 
    Typical value (May vary, depending on 
      system) |  
  
    | %ALLUSERSPROFILE% | 
    C:\ProgramData | 
    C:\Documents and Settings\All Users |  
  
    | %APPDATA% | 
    C:\Users\(username}\AppData\Roaming | 
    C:\Documents and Settings\{username}\Application 
      Data |  
  
    | %CommonProgramFiles% | 
    C:\Program Files\Common Files | 
      |  
  
    | %COMPUTERNAME% | 
    {computername} | 
    {computername} |  
  
    | %COMSPEC% | 
    C:\Windows\System32\cmd.exe | 
    C:\Windows\System32\cmd.exe |  
  
    | %HOMEDRIVE% | 
    C: | 
    C: |  
  
    | %HOMEPATH% | 
    \Users\{username} | 
    \Documents and Settings\{username} |  
  
    | %LOCALAPPDATA% | 
    C:\Users\{username}\AppData\Local | 
      |  
  
    | %PATH% | 
    C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem | 
    C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem |  
  
    | %PATHEXT% | 
    .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS 
      ; .WSF; .WSH; .MSC | 
    .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF; 
      .WSH |  
  
    | %ProgramData% | 
    C:\ProgramData | 
      |  
  
    | %PROGRAMFILES% | 
    Directory containing program files, 
      usually C:\Program Files | 
    Directory containing program files, usually C:\Program 
      Files |  
  
    | %PROMPT% | 
    Code for current command prompt format. 
      Code is usually $P$G | 
    Code for current command prompt format. Code is usually 
      $P$G |  
  
    | %Public% | 
    C:\Users\Public | 
      |  
  
    | %SYSTEMDRIVE% | 
    The drive containing the Windows XP 
      root directory, usually C: | 
    The drive containing the Windows XP root directory, 
      usually C: |  
  
    | %SYSTEMROOT% | 
    The Windows XP root directory, 
      usually C:\Windows | 
    The Windows XP root directory, usually 
      C:\Windows |  
  
    | %TEMP% and %TMP% | 
    C:\Users\{Username}\AppData\Local\Temp | 
    C:\DOCUME~1\{username}\LOCALS~1\Temp |  
  
    | %USERNAME% | 
    {username} | 
    {username} |  
  
    | %USERPROFILE% | 
    C:\Users\{username} | 
    C:\Documents and Settings\{username} |  
  
    | %WINDIR% | 
    C:\Windows | 
    C:\Windows |    
  
  
     | 
     | 
     |  
  
    | Variable | 
    Value | 
     |  
  
    | %DATE% | 
    Current date in the format 
      determined by the Date 
      command | 
     |  
  
    | %TIME% | 
    Current time in the format 
      determined by the Time 
      command | 
     |  
  
    | %CD% | 
    Current directory with its full 
      path | 
     |  
  
    | %ERRORLEVEL% | 
    Number defining exit status of a 
      previous command or program | 
     |  
  
    | %RANDOM% | 
    Random number between 0 and 
    32767 | 
     |   
 
  
  
  
  
  
  
  
  
  
  
 
How to get day of week, day of month, month and 
year values in a Windows DOS batch script? 
  
  If you use batch scripts in Windows to run your backups, 
  you’ll probably find it useful to have date information available for naming 
  your backup files. There is a really easy way to get date related 
  information in a batch script : 
  DAY OF WEEK 
  set dow=%date:~0,3% 
  This returns Mon to Sun. 
    
  DAY OF MONTH 
  set dom=%date:~7,2% 
  This returns a value between 01 to 31 
    
  MONTH OF YEAR 
  set moy=%date:~4,2% 
  This returns a value between 01 to 12. 
    
  YEAR 
  set year=%date:~10,4% 
  Returns for example, 2008. 
    
  Date Only 
  set MMDDCCYY=%date:~4,10% 
  Returns for example, 01/01/2008. 
     
  
Close open file(s) and backup 
  
 net files | findstr /i "myfile.ext"
for /F %%a in ('net files ^|findstr /i "myfile.ext"') do net files %%a /close
rem -- for backing up dsswX.ext (security file for SDS, not needed for SSCS)
set WHATDIRTOBACKUP=\\servername\sharename\directory\
set ZIPLOC=\\servername\sharename\backupdirectory\
set CCYYMMDD=%date:~10,4%%date:~7,2%%date:~4,2%
copy %WHATDIRTOBACKUP%myfile.ext %ZIPLOC%myfile-%CCYYMMDD%.ext
pause
rem example from 
rem net files | findstr /i "amc.*\.exe"
rem for /F %%a in ('net files ^|findstr /i "\\amc.*\.exe"') do net files %%a /close
rem net files | findstr /i "amc.*\.dat"
rem for /F %%a in ('net files ^|findstr /i "\\amc.*\.dat"') do net files %%a /close
rem net files | findstr /i "muparser.dll"
rem for /F %%a in ('net files ^|findstr /i "muparser.dll"') do net files %%a /close
 
loop off an input 
file @echo off rem  rem This batch file will copy files 
from a server to a client rem set 
serverupdatepath=\\SERVERNAME\SHARE\DIR\ set inputfilename=%1 
if "%1" == "" ( echo. echo Input file is blank. call as 
NAMEOFBATCH.BAT input.txt echo. echo To create a file of computers use: 
NET VIEW ^> c:\computerlist.txt echo. echo 
%serverupdatepath% echo. goto EOF ) 
if not exist %1 ( echo.Input file invalid. goto 
EOF ) 
goto DOIT 
:EOF pause exit /B 
:DOIT for /f %%i in (%inputfilename%) do call :ACTIONS 
%%i goto EOF 
:ACTIONS echo.%1 xcopy /y /d %serverupdatepath%*.*  
\\%1\c$\CLIENTDIR\*.* 
---end batch file to loop off inputfile
  
  
 
 copy certain files found under a 
dir set dSource=C:\Main directory\sub 
directory set dTarget=D:\Documents set fType=*.doc for /f "delims=" %%f 
in ('dir /a-d /b /s "%dSource%\%fType%"') do (     copy /V 
"%%f" "%dTarget%\" 2>nul ) 
  
real example 
rem 
---------------------------------------------------------------------------- set 
DIRTOPUTDATA=\\servername\sharename\%username%\email\ set 
DIRTOPUTDOCS=\\servername\sharename\%username%\docs\ 
@echo. @echo. @echo copy pst files...just the PSTs @echo  
from:%USERPROFILE% to:%DIRTOPUTDATA% @echo 
. @echo      Do NOT start OUTLOOK until this is 
done. @echo . rem  rem ***NOTE: this does not make duplicate 
distinctions, so if there is 2 
with rem          same name, 
then the last one found will be the one. rem  set fType=*.pst for /f 
"delims=" %%f in ('dir /a-d /b /s "%USERPROFILE%\%fType%"') do 
(     xcopy "%%f" "%DIRTOPUTDATA%" /y/d 2>nul )
  
  
 
Check if ran today, and exit out 
*** this will stamp the file with the date, and append to the 
file 
  
set 
lastrunfile=\\servername\sharename\%username%\lastrun.txt 
rem 
---------------------------------------------------------------------------- @echo. @echo....Checking 
for backup already executed today...if so exit @echo. if not exist 
%lastrunfile% echo 00/00/0000 > %lastrunfile% (for /f %%a in 
(%lastrunfile%) do set LastRun=%%a) 2>>nul if 
"%LastRun%"=="%date:~4,10%" (goto endit) else (echo 
%date:~4,10%>>%lastrunfile%) 
  
rem do whatever you need to do here 
  
:endit @echo. @echo. @echo All 
done.............................. rem pause exit 
  
  
 
 
  repetitive/recursive subdirectories removal batch 
file
Ok, if you have a recursive subdirectory problem with a never ending 
repetitive subdirectories that you cant delete with explorer or DOS…here ya 
go I know looking at this batch file, your going “this aint gonna do 
anything” 
  
DelFolder.bat contains: @echo off if 
{%1}=={} @echo Syntax: DelFolder FolderPath&goto :EOF if not exist %1 
@echo Syntax: DelFolder FolderPath - %1 NOT found.&goto 
:EOF setlocal set folder=%1 set MT="%TEMP%\DelFolder_%RANDOM%" MD 
%MT% RoboCopy %MT% %folder% /MIR RD /S /Q %MT% RD /S /Q %folder% rem 
end batchfile 
-------------------------------------- Robocopy explained Usage :: 
ROBOCOPY source destination [file [file]...] [options] /MIR :: MIRror a 
directory tree (equivalent to /E plus /PURGE) 
So, The batch file is creating a temp directory which has nothing in 
it Robocopy then use that temp directory as the SOURCE (which is blank right) 
and the /MIR option says make the DESTINATION look like the source 
  
 
Check OS for setting of users directory 
  
SET version=Unknown 
VER | find /i "5.0." > nul  IF %ERRORLEVEL% EQU 0 set 
version=2000 VER | find /i "5.1." > nul  IF %ERRORLEVEL% EQU 0 set 
version=XP VER | find /i "5.2." > nul  IF %ERRORLEVEL% EQU 0 set 
version=2003 VER | find /i "6.0." > nul  IF %ERRORLEVEL% EQU 0 set 
version=Vista-7 VER | find /i "6.1." > nul  IF %ERRORLEVEL% EQU 0 set 
version=Vista-7 
 @echo. @echo. @echo   Your OS is = 
%version% @echo. @echo. if %version%==Vista-7 ( set 
MYDOCSDIR=%userprofile%\Documents\ ) else ( set MYDOCSDIR=%userprofile%\My 
Documents\ )
  
  
  
 
 Different Ways to check 
version 
  
@echo off set version=NONE VER | find /i "5.0." > nul && set 
version=2000 VER | find /i "5.1." > nul && set version=XP VER | 
find /i "5.2." > nul && set version=2003 VER | find /i "6.0." > 
nul && set version=Vista VER | find /i "6.1." > nul && set 
version=Win7 VER | find /i "6.2." > nul && set version=Win8 VER 
| find /i "6.3." > nul && set version=Win8 echo %version% set 
version=NONE if %version% == NONE ( systeminfo | find /i "Microsoft Windows 
XP" > nul && set version=WinXP )  if %version% == NONE ( 
systeminfo | find /i "Microsoft Windows Vista" > nul && set 
version=WinVista ) if %version% == NONE ( systeminfo | find /i "Microsoft 
Windows 7" > nul && set version=Win7 ) if %version% == NONE ( 
systeminfo | find /i "Microsoft Windows 8" > nul && set version=Win8 
) if %version% == NONE ( systeminfo | find /i "Microsoft Windows Server 2000" 
> nul && set version=WinServer2000 ) if %version% == NONE ( 
systeminfo | find /i "Microsoft Windows Server 2003" > nul && set 
version=WinServer2003 ) if %version% == NONE ( systeminfo | find /i 
"Microsoft Windows Server 2008" > nul && set version=WinServer2008 
) if %version% == NONE ( systeminfo | find /i "Microsoft Windows Server 2012" 
> nul && set version=WinServer2012 ) echo %version% pause 
  
  
  
 
  
Example of variables of files 
taken from - http://zoril.co.uk/stuff/batchvars.php
  
@echo OFF rem -- By Lewis rem -- zoril.co.uk echo 
-------------- echo - THIS  FILE - echo 
-------------- echo. rem - drive of file echo [ Drive ][ %%~d0 ] 
%~d0 rem - path of file [ no filename or drive ] echo [ Path  ][ 
%%~p0 ] %~p0 rem - path of file echo [ Path  ][   %%0 ] 
%0 echo [ Path  ][ %%~f0 ] %~f0 rem - short path of file echo [ 
Short ][ %%~s0 ] %~s0 rem - name of file [ no extension ] echo [ 
Name  ][ %%~n0 ] %~n0 rem - extension of file echo [ Exten ][ %%~x0 ] 
%~x0 rem - modified time of file echo [ Time  ][ %%~t0 ] %~t0 rem 
- bytesize of file echo [ Size  ][ %%~z0 ] 
%~z0 echo. echo. echo -------------- echo - INPUT FILE - echo 
-------------- echo. rem - drive of file echo [ Drive ][ %%~d1 ] 
%~d1 rem - path of file [ no filename or drive ] echo [ Path  ][ 
%%~p1 ] %~p1 rem - path of file echo [ Path  ][   %%1 ] 
%1 echo [ Path  ][ %%~f1 ] %~f1 rem - short path of file echo [ 
Short ][ %%~s1 ] %~s1 rem - name of file [ no extension ] echo [ 
Name  ][ %%~n1 ] %~n1 rem - extension of file echo [ Exten ][ %%~x1 ] 
%~x1 rem - modified time of file echo [ Time  ][ %%~t1 ] %~t1 rem 
- bytesize of file echo [ Size  ][ %%~z1 ] %~z1 
  
echo. echo. echo. rem a line with an a in echo %~a1 
 pause > nul 
  
  
 
  
  
    | The Current File |  
  
    | Percent Code | 
    Result | 
    Example |  
  
    | %~d0 | 
    Drive | 
    H: |  
  
    | %~p0 | 
    Path (No Drive) | 
    \Notes\ |  
  
    | %0 | 
    Full Quoted Path | 
    "H:\Notes\batch_variables.bat" |  
  
    | %~f0 | 
    Full Path | 
    H:\Notes\batch_variables.bat |  
  
    | %~s0 | 
    8.3 Short Path | 
    H:\Notes\BATCH_~1.bat |  
  
    | %~n0 | 
    Filename (No Extension) | 
    batch_variables |  
  
    | %~x0 | 
    File Extension | 
    .bat |  
  
    | %~t0 | 
    Modified Time | 
    10/12/2009 05:46 |  
  
    | %~z0 | 
    File Size (bytes) | 
    1233 |  
  
    |   |  
  
    | The Input File |  
  
    | Percent Code | 
    Result | 
    Example |  
  
    | %~d1 | 
    Drive | 
    C: |  
  
    | %~p1 | 
    Path (No Drive) | 
    \Program Files (x86)\Mozilla Firefox\ |  
  
    | %1 | 
    Full Quoted Path | 
    "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" |  
  
    | %~f1 | 
    Full Path | 
    C:\Program Files (x86)\Mozilla Firefox\firefox.exe |  
  
    | %~s1 | 
    8.3 Short Path | 
    C:\PROGRA~2\MOZILL~1\firefox.exe |  
  
    | %~n1 | 
    Filename (No Extension) | 
    firefox |  
  
    | %~x1 | 
    File Extension | 
    .exe |  
  
    | %~t1 | 
    Modified Time | 
    28/10/2008 14:46 |  
  
    | %~z1 | 
    File Size (bytes) | 
    908280 |   
  
  
  
 
  
Backup batch file for a PC to wherever (mainly designed to backup to 
a server and share) 
@echo off set DOW=%date:~0,3% set dom=%date:~7,2% set 
moy=%date:~4,2% set year=%date:~10,4% 
set skipdocs=Y 
rem set DIRTOPUTDATA=c:\temp\test\email\ rem set 
DIRTOPUTDOCS=c:\temp\test\docs\ set 
DIRTOPUTDATA=\\servername\sharename\%username%\email\ set 
DIRTOPUTDOCS=\\servername\sharename\%username%\docs\ 
set lastrunfile=\\servername\sharename\%username%\lastrun.txt 
rem 
---------------------------------------------------------------------------- @echo. @echo....Checking 
for backup already executed today...if so exit @echo. if not exist 
%lastrunfile% echo 00/00/0000 > %lastrunfile% (for /f %%a in 
(%lastrunfile%) do set LastRun=%%a) 2>>nul if 
"%LastRun%"=="%date:~4,10%" (goto endit) else (echo 
%date:~4,10%>>%lastrunfile%) 
rem pause 
rem 
---------------------------------------------------------------------------- @echo. @echo. @echo....Backing 
up your personal Docs to server @echo. @echo. @echo   setting 
my docs directory 
SET version=Unknown 
VER | find /i "5.0." > nul  IF %ERRORLEVEL% EQU 0 set 
version=2000 VER | find /i "5.1." > nul  IF %ERRORLEVEL% EQU 0 set 
version=XP VER | find /i "5.2." > nul  IF %ERRORLEVEL% EQU 0 set 
version=2003 VER | find /i "6.0." > nul  IF %ERRORLEVEL% EQU 0 set 
version=Vista-7 VER | find /i "6.1." > nul  IF %ERRORLEVEL% EQU 0 set 
version=Vista-7 
 @echo. @echo. @echo   Your OS is = 
%version% @echo. @echo. if %version%==Vista-7 ( set 
MYDOCSDIR=%userprofile%\Documents\ ) else ( set MYDOCSDIR=%userprofile%\My 
Documents\ ) 
rem 
---------------------------------------------------------------------------- @echo. @echo. @echo 
create the dirs if they dont exist if not exist %DIRTOPUTDATA% md 
%DIRTOPUTDATA% 
rem 
---------------------------------------------------------------------------- @echo. @echo. @echo 
killing outlook taskkill /im outlook.exe 
rem 
---------------------------------------------------------------------------- rem 
waste some time waiting for outlook to die @ping 127.0.0.1 -n 2 -w 1000 > 
nul @ping 127.0.0.1 -n 2 -w 1000> nul 
rem 
---------------------------------------------------------------------------- @echo. @echo. @echo 
copy pst files...just the PSTs @echo  from:%USERPROFILE% 
to:%DIRTOPUTDATA% @echo . @echo      Do NOT start 
OUTLOOK until this is done. @echo . rem  rem ***NOTE: this does not 
make duplicate distinctions, so if there is 2 
with rem          same name, 
then the last one found will be the one. rem  set fType=*.pst for /f 
"delims=" %%f in ('dir /a-d /b /s "%USERPROFILE%\%fType%"') do 
(     xcopy "%%f" "%DIRTOPUTDATA%" /y/d 2>nul ) 
  
rem backup the autofill file from Outlook 2007 set fType=*.nk2 for /f 
"delims=" %%f in ('dir /a-d /b /s "%USERPROFILE%\%fType%"') do 
(     xcopy "%%f" "%DIRTOPUTDATA%" /y/d 2>nul ) 
 if %skipdocs% == Y ( goto endit )      rem 
---------------------------------------------------------------------------- @echo. @echo. @echo 
copy the documents directory from:%MYDOCSDIR% to:%DIRTOPUTDOCS% rem  rem 
%USERPROFILE% robocopy %MYDOCSDIR% %DIRTOPUTDOCS% *.* /MIR /r:0 
:endit @echo. @echo. @echo All 
done.............................. rem pause exit 
   
----end batch 
file-----------------------------------------------     
  
  
 
  
  
     
 
You are here: Home-Computer Tips & Help-Programming-Batch Files 
Previous Topic: SQL RPG examples  
   |