set /P val=Please enter value: || set val=default
If the user enters a value, the %val% variable is set to the given value. Otherwise (if the user simply pressed Enter) 'default' is used instead.
Neat :).
Thoughts?
set /P val=Please enter value: || set val=default
set oldp=[define original folder here]
set newp=[define destination folder here]
attrib -h -s %oldp%
move %oldp% %newp%
attrib +h +s %newp%
attrib -h -s %oldp% /S /D
set oldp=[define original path here]
set newp=[define destination here]
xcopy %oldp% %newp% /H /S
@echo off
setlocal enabledelayedexpansion
:init_vars
set ini_file=my.ini
goto show_values
:show_values
for /F "tokens=1,2 delims==" %%L in (%ini_file%) do (
if not [%%M]==[] (
echo Value: %%M
)
)
goto eof
:eof
echo Press any key to quit
pause > NUL
[provider]
name=ETH
organization=Stardust
[file]
server=192.168.254.42 ; use IP address
port=8080
file=wonderful.txt
@echo off
setlocal enabledelayedexpansion
:init_vars
:: this is our ini file path
set ini_file=my.ini
:: this is the key for which we want to retrieve a value
set key=server
:: this is the length of our key word (e.g. 'server')
set keylen=6
:: this is the index at which the equal sign should be
set /a eqsign=%keylen%+1
:: tmp dir name (should be empty)
set tmp_dir=tmp000
:: make temp dir
if not exist %tmp_dir% mkdir %tmp_dir%
goto get_key
:get_key
set tmp_file=%tmp_dir%\tmp.txt
:: this read the first token of every line, shouldn't be spaces in the ini file properties
for /F %%L in (%ini_file%) do (
set line=%%L
set linestart=!line:~0,%keylen%!
if !linestart!==%key% (
set value=!line:~%eqsign%!
goto output_value
)
)
:: this will erase existing %tmp_dir%\new.ini and %tmp_dir%\new.txt files
:output_value
echo Outputting value to ini file
echo server=%value% > %tmp_dir%\new.ini
echo Outputting value to text file
echo The value is: %value% > %tmp_dir%\new.txt
goto eof
:eof
echo Press any key to quit
pause > NUL
goto blackhole
:blackhole
@echo off
:init
setlocal enabledelayedexpansion
set out_dir=site
set page_sub=pages
set page_dir=%out_dir%\%page_sub%
if not exist %page_dir% mkdir %page_dir%
goto create_site
:create_site
set ifile=%out_dir%\index.html
> %ifile% echo ^<html^>^<head^>^<title^>Commands index^</title^>^</head^>
>> %ifile% echo ^<body^>
for /F "tokens=1" %%C in ('help') do (
call :set_is_command %%C
if "!is_command!"=="1" (
echo Handling: %%C
set cfile=%page_dir%\%%C.html
>> %ifile% echo ^<a href="%page_sub%\%%C.html"^>%%C^</a^>^<br/^>
:: set cfile=CON
> !cfile! echo ^<html^>^<head^>^<title^>%%C^</title^>^</head^>
>> !cfile! echo ^<body^>^<h2^>%%C^</h2^>^<pre^>
for /F "delims=" %%T in ('help %%C') do (
set str=%%T
set str=!str:…=^à!
set str=!str:‚=^é!
set str=!str:Š=^è!
set str=!str:ˆ=^ê!
set str=!str:‰=^ë!
set str=!str:Œ=^î!
set str=!str:‹=^ï!
set str=!str:“=^ô!
set str=!str:—=^ù!
set str=!str:–=^û!
set str=!str:‡=^ç!
set str=!str:ÿ=^ !
>> !cfile! echo !str!
)
>> !cfile! echo ^<br/^>^<br/^>
>> !cfile! echo ^<a href="../index.html"^>Back to index/Retour ^à l'index^</a^>
>> !cfile! echo ^</pre^>^</body^>^</html^>
)
)
>> %ifile% echo ^</body^>
>> %ifile% echo ^</html^>
goto end
:set_is_command
set name=%~1
set is_command=1
if not "%name%"=="" (
set num=1
for /F "usebackq tokens=1 delims=ABCDEFGHIJKLMNOPQRSTUVWXYZ" %%W IN ('%name%') do (
set num=0
)
set is_command=!num!
)
goto blackhole
:end
echo Press any key to quit...
pause > NUL
:blackhole
cmd> help
cmd> help for
@echo off
:init_vars
if [%1]==[] goto what_file
if not exist %1 goto what_file
goto show_info
:show_info
echo.
echo General information
echo -------------------
echo Fully qualified name (%%~f1): %~f1
echo Drive letter (%%~d1): %~d1
echo Path (%%~p1): %~p1
echo File name (%%~n1): %~n1
echo File extension (%%~x1): %~x1
echo Date time (%%~t1): %~t1
echo Size (%%~z1): %~z1
echo File attributes (%%~a1): %~a1
goto show_combinations
:show_combinations
echo.
echo Some possible combinations
echo --------------------------
echo Drive and path (%%~dp1): %~dp1
echo Filename and extension (%%~nx1): %~nx1
echo Path using short names (%%~fs1) : %~fs1
echo.
goto eof
:what_file
echo Please provide valid file.
goto eof
:eof
echo Press any key to quit
pause > NUL
@echo off
call info test_file_info.longext
@echo off
:action
set num=0
for /l %%N IN (1,1,9) do (
echo %num%
set /a num=%num%+1
)
goto show_result
:show_result
echo Num is actually now worth: %num%
goto eof
:eof
echo Press any key to quit...
pause > NUL
goto blackhole
:blackhole
@echo off
:init_sys
setlocal enabledelayedexpansion
:action
set num=0
for /l %%N IN (1,1,9) do (
echo !num!
set /a num=!num!+1
)
goto show_result
:show_result
echo Num is actually now worth: %num%
goto eof
:eof
echo Press any key to quit...
pause > NUL
goto blackhole
:blackhole
@echo off
:init_sys
setlocal enabledelayedexpansion
:init_vars
set text=The quick brown fox jumps over the lazy dog
:action
echo Displaying text using %%: %text%
echo Displaying text using ^^!: !text!
for /l %%N IN (1,1,9) do (
call :get_substr %%N "%text%"
echo Token: !strtok!
)
goto eof
:get_substr
set token=%~1
set string=%~2
for /F "usebackq tokens=%token% delims= " %%w in ('!string!') do (
set strtok=%%w
)
goto blackhole
:eof
echo Press any key to quit...
pause > NUL
goto blackhole
:blackhole
@echo off
:init_sys
setlocal enabledelayedexpansion
:init_vars
set text=The quick brown fox jumps over the lazy dog
:action
for /l %%N IN (1,1,9) do (
call :getchars %%N
)
goto eof
:getchars
set num=%~1
echo !text:~0,%num%!
goto blackhole
:eof
echo Press any key to quit...
pause > NUL
goto blackhole
:blackhole
@echo off
:create_file
call :get_timestamp
type NUL > %timestamp%.file
goto eof
:get_timestamp
set d_stamp=%date:~11%%date:~8,2%%date:~5,2%
set t_stamp=%time:~0,2%%time:~3,2%%time:~6,2%%time:~9%
set timestamp=%d_stamp%%t_stamp%
goto blackhole
:eof
echo Press any key to quit...
pause > NUL
goto blackhole
:blackhole
@echo off
:get_year
echo System date: %date%
echo System month: %date:~8,2%
echo System year: %date:~11%
goto eof
:eof
echo Press any key to quit...
pause > NUL
goto blackhole
:blackhole
@echo off
:show_date
call :get_date / 0 1
echo Date: %_date%
goto eof
:get_date
set delim=%~1
set order=%~2
set show_day=%~3
echo delim=%delim% and order=%order% and show_day=%show_day%
for /F "tokens=1,2,3,4 delims=/ " %%d in ('date /T') do (
set day=%%d
set dd=%%e
set mm=%%f
set yyyy=%%g
)
set _date=%dd%%delim%%mm%%delim%%yyyy%
if "%order%"=="1" set _date=%mm%%delim%%dd%%delim%%yyyy%
if "%show_day%"=="1" set _date=%day% %_date%
goto blackhole
:eof
echo Press any key to quit...
pause > NUL
goto blackhole
:blackhole
@echo off
:get_date
for /F "tokens=2 delims= " %%d in ('date /T') do (
set _date=%%d
)
goto blackhole
:blackhole
@echo off
:show_date
call :get_date
echo Date: %_date%
goto eof
:get_date
for /F "tokens=2 delims= " %%d in ('date /T') do (
set _date=%%d
)
goto blackhole
:eof
echo Press any key to quit...
pause > NUL
goto blackhole
:blackhole
@echo off
:action
for /D %%l in (*) do (
echo Directory: %%l
)
goto eof
:eof
echo Press any key to close window...
pause > nul
goto blackhole
:blackhole
@echo off
:action
for /D %%l in (*) do (
echo Directory: %%l
cd %~dp0\%%l
for %%f in (*.*) do (
echo File: %%f
)
)
goto eof
:eof
echo Press any key to close window...
pause > nul
goto blackhole
:blackhole
@echo off
:init_vars
if exist file1.txt goto action
echo Oops. file1.txt doesn't exist. Please create it or change file name.
goto eof
:action
for /F %%l in (file1.txt) do (
echo Content: %%l
)
goto eof
:eof
echo Press any key to close window...
pause > nul
goto blackhole
:blackhole
@echo off
:init_vars
if exist file1.txt goto action
echo Oops. file1.txt doesn't exist. Please create it or change file name.
goto eof
:action
for /F "delims=" %%l in (file1.txt) do (
echo Content: %%l
)
goto eof
:eof
echo Press any key to close window...
pause > nul
goto blackhole
:blackhole
@echo off
:init_vars
if exist file1.txt goto action
echo Oops. file1.txt doesn't exist. Please create it or change file name.
goto eof
:action
set /P file_content=<file1.txt
for %%l in (%file_content%) do (
echo Content: %%l
)
goto eof
:eof
echo Press any key to close window...
pause > nul
goto blackhole
:blackhole
@echo off
:init_vars
set /P file_content=<file1.txt
goto action
:action
echo File content: %file_content%
goto eof
:eof
echo Press any key to close window...
pause > nul
goto blackhole
:blackhole
@echo off
:param_check
if "" == "%1" goto invalid_call
if "" == "%2" goto invalid_call
goto file_one
:file_one
if exist %1 goto file_two
set file_path=%1
goto show_error
:file_two
if exist %2 goto run_content
set file_path=%2
goto show_error
:run_content
if %~z1==%~z2 goto same_size
if %~z1 LSS %~z2 goto first_smaller
goto second_smaller
:same_size
echo Both files are of same size.
goto eof
:first_smaller
echo %1 is smaller than %2
goto eof
:second_smaller
echo %2 is smaller than %1
goto eof
:show_error
echo File not found: "%file_path%"
goto eof
:invalid_call
echo Please call with the following parameters:
echo [1] the first file name
echo [2] the second file name
goto eof
:eof
echo Press any key to close window...
pause > nul
goto blackhole
:blackhole
@echo off
call comp_files file1.txt file2.txt
@echo off
:action
echo File size of currently running batch file: %~z0
goto eof
:eof
echo Press a key to quit...
pause > NUL
goto blackhole
:blackhole

