Some useful Windows Batch scripts
Assigning output of a program to a variable
@echo off
setlocal enabledelayedexpansion
REM: Here we run the program "hostname"
hostname > HOSTNAME.tmp
set /p HOSTNAME=< HOSTNAME.tmp
echo %HOSTNAME%
Getting machine time
@echo off
setlocal enabledelayedexpansion
set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
echo hour=%hour%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%
echo min=%min%
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
echo secs=%secs%
Getting machine date
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set datestr=%month%_%day%_%year%
echo datestr is %datestr%
Converting UPPER CASE letters to lower case by perl
@echo off
setlocal enabledelayedexpansion
set LOWER_CASE="I AM AN UPPER CASE"
@FOR /F %%s IN ('perl -e "print lc(pop)" %LOWER_CASE%') DO @set LC_LOWER_CASE=%%s
echo %LOWER_CASE%
Upload files by WinSCP parallel
For details, please refer https://winscp.net/eng/docs/scripting
@echo off rem Set up values set SESSION=My_Session_name set REMOTE_PATH=/my/ftp/remote/path set LOCAL_DIR=C:\my\local\dir\ rem Get list of files dir %LOCAL_DIR% /b /a-d > list.txt rem Generate WinSCP commands for each line in list file for /F %%i in (list.txt) do ( start /b cmd /c winscp.com /command "open %SESSION%" "put "%LOCAL_DIR%\%%i" "%REMOTE_PATH%"" "close" "exit" ) set RESULT=%ERRORLEVEL% del list.txt exit /b %RESULT% REM pause
Upload files by WinSCP parallel but limit
For details, please refer https://winscp.net/eng/docs/scripting
@echo off setlocal EnableDelayedExpansion rem Set up values set SESSION=My_Session_name set REMOTE_PATH=/my/ftp/remote/path set LOCAL_DIR=C:\my\local\dir\ REM Run only 3 sessions at same time set /A LIMIT=3 REM Timeout in seconds set INTERVAL=60 rem Get list of files dir %LOCAL_DIR% /b /a-d > list.txt set "cmd=findstr /R /N "^^" list.txt | find /C ":"" for /f %%a in ('!cmd!') do set number=%%a echo Number of files is %number% rset /A COUNTER=0 for /F %%i in (list.txt) do ( REM if %%i == "" () set /A COUNTER=COUNTER+1 echo !COUNTER! if !COUNTER! LSS %LIMIT% ( Call :RUN_WINSCP %%i ) ELSE ( echo Over LIMIT %LIMIT% echo Wait REM Try a loop to check if a session closed then open a new session Call :TIMEOUT %%i ) ) set RESULT=%ERRORLEVEL% del list.txt del count_number_of_sessions rem Propagating WinSCP exit code exit /b %RESULT% REM pause REM Functions here :RUN_WINSCP rem Generate WinSCP commands for each line in list file start cmd /k echo start cmd /c winscp.com /command "open %SESSION%" "put "%LOCAL_DIR%\%~1" "%REMOTE_PATH%"" "close" "exit" goto:eof :NUMBER_OF_SESSIONS REM tasklist|find /i /c "cmd.exe" > count_number_of_sessions tasklist|find /i /c "WinSCP.com" > count_number_of_sessions set /p NUMBER_OF_SESSIONS=< count_number_of_sessions REM set /A NUMBER_OF_SESSIONS=NUMBER_OF_SESSIONS-1 set /A NUMBER_OF_SESSIONS=NUMBER_OF_SESSIONS+16 echo Number of SESSIONS are !NUMBER_OF_SESSIONS! set /A "%~1=!NUMBER_OF_SESSIONS!" goto:eof :TIMEOUT echo Limited %LIMIT%, retrying a new session for %~1 in %INTERVAL% seconds... timeout /t %INTERVAL% Call :RE_TRY %~1 goto:eof :RE_TRY Call :NUMBER_OF_SESSIONS NUMBER_OF_SESSIONS if !NUMBER_OF_SESSIONS! LSS %LIMIT% ( echo New Session for %~1 Call :RUN_WINSCP %~1 ) ELSE ( echo Continue Waiting for %~1 Call :TIMEOUT %~1 ) goto:eof