DOS BATCH SCRIPTS

1. Had to frequently pull files from a server. The file set  used to be in following format.

abc123_file1.txt
def23145_file2.txt
r4rtyyy_file3.txt

The task was to remove the prefix from names and put the file set back. It was too mechanical, so I wrote following script to do the same.


ECHO OFF

SETLOCAL EnableDelayedExpansion

FOR %%I IN (*) DO (
   REM ECHO %%I
   SET  xname=%%I
 
   for /f "tokens=1,2 delims=_" %%a in ("%%I") do (
     REM ECHO %%a
     REM ECHO %%b  
     REM ECHO !xname!
     SET xname=!xname:%%a_=!
     REM ECHO !xname!
     SET ENDSWITH=!xname:~-4!
     REM ECHO !ENDSWITH!
     IF "!ENDSWITH!"==".csv"  REN %%I  !xname!
     IF "!ENDSWITH!"=="done"  REN %%I  !xname!
   )
)


2.  I faced  a situation where I needed to upload files to server at some intervals and not in one go. I was looking for a way to write script. This is what I did.  I was using winSCP  to upload the file.  I came up with following batch file.

ECHO OFF

SETLOCAL EnableDelayedExpansion

FOR %%I IN (*) DO (
   REM ECHO %%I
   SET  xname=%%I
   SET ENDSWITH=!xname:~-5!
   ECHO !xname!
   copy wscp.txt wscp2.txt
   IF "!ENDSWITH!"==".sgnl"  (
     ECHO Put D:\Users\gbairwa\Documents\reprocess\poc\!xname!  /dest/path/>> wscp2.txt
     ECHO close>>wscp2.txt
     ECHO exit>>wscp2.txt
   )
   type wscp2.txt
   # pause
   "C:\Program Files (x86)\WinSCP\winscp.com" /ini=nul /script=wscp2.txt
   # Below command to introduce some wait/sleep
   ping 192.0.2.2 -n 1 -w 100000 > nul
)

wsp.txt
--------
open -hostkey="ssh-rsa 2048 f8:c3:55:ef:df:45:fc:48:89:48:ac:e8:d5:70:1f:8d" userid:password@host.domain
option transfer binary

Comments

Popular posts from this blog

SQL

Analytics

DBeaver