Search code examples
batch-fileftprename

How to write a batch/script to rename FTP file by a list of choices


I'm trying to write a Windows 7 script for my boss, where the following happens;

  1. Display a list of choices based on directories within an FTP directory

  2. When he selects one it will cd to that directory

  3. Then display another list of choices based on files ending in .htm

  4. When he selects one it renames the first 8 characters with today's date yyyymmdd

All .htm files have a yyyymmdd format in the beginning of the file name

What language should I use? Any ideas where to start something like this?

this is what I have so far

    echo off
    cls
    echo user me@company.com> ftpcmd.dat
    echo password>> ftpcmd.dat
    echo mls . 1.tmp>> ftpcmd.dat
    echo y >> ftpcmd.dat
    echo quit>> ftpcmd.dat
    ftp -n -s:ftpcmd.dat ftp.company.com
    del ftpcmd.dat
    
    type mls.tmp | more +2 > trim.tmp
    findstr /N "^" trim.tmp > dirs.tmp
    
    ::code need to make choices from dirs.tmp
    ::value of selected to be %dir%
    
    cls
    echo user me@company.com> ftpcmd.dat
    echo password>> ftpcmd.dat
    echo cd %dir%>> ftpcmd.dat
    echo mls . mls.tmp>> ftpcmd.dat
    echo y >> ftpcmd.dat
    echo quit>> ftpcmd.dat
    ftp -n -s:ftpcmd.dat ftp.company.com
    del ftpcmd.dat
    
    findstr ".htm" mls.tmp > trim.tmp
    findstr /N "^" trim.tmp > files.tmp
    
    ::code need to make choices from files.tmp
    ::value of selected to be %sel%
    
    set new=%date:~10,4%%date:~4,2%%date:~7,2%%sel:~9%
    
    cls
    echo user me@company.com> ftpcmd.dat
    echo password>> ftpcmd.dat
    echo cd %dir%>> ftpcmd.dat
    echo rename %sel% %new%>> ftpcmd.dat
    echo quit>> ftpcmd.dat
    ftp -n -s:ftpcmd.dat ftp.company.com
    del ftpcmd.dat
    del *.tmp

I firgured once I can fill in the missing pieces, I can loop it to cut down on the code.


Solution

  • Ok, just test this for now and tell me if there are any problems.

    Code:

    echo off
    setlocal enabledelayedexpansion
    cls
    echo user me@company.com> ftpcmd.dat
    echo password>> ftpcmd.dat
    echo mls . 1.tmp>> ftpcmd.dat
    echo y >> ftpcmd.dat
    echo quit>> ftpcmd.dat
    ftp -n -s:ftpcmd.dat ftp.company.com
    del ftpcmd.dat
    
    type mls.tmp | more +2 > trim.tmp
    findstr /N "^" trim.tmp > dirs.tmp
    
    :invalid
    set count=0
    Echo Directories:
    Echo.
    for /f %%a in (dirs.tmp) do (
    set /a count+=1
    Echo     !count!. %%a
    )
    Echo.
    set /p "choice=Enter directory number: "
    if "%choice%" equ "" (Echo Invalid Choice... &pause&cls&goto :invalid)
    if %choice% lss "%count%" (Echo Invalid Choice... &pause&cls&goto :invalid)
    if %choice% lss 0 (Echo Invalid Choice... &pause&cls&goto :invalid)
    
    set count=0
    set dir=null_value
    for /f %%a in (dirs.tmp) do (
    set /a count+=1
    if !count!==!choice! set dir=%%a
    )
    cls
    if "%dir%"=="null_value" (
    Echo Error: direcotry finding
    pause&Exit)
    cls
    Echo Selected %dir%
    pause
    cls
    echo user me@company.com> ftpcmd.dat
    echo password>> ftpcmd.dat
    echo cd %dir%>> ftpcmd.dat
    echo mls . mls.tmp>> ftpcmd.dat
    echo y >> ftpcmd.dat
    echo quit>> ftpcmd.dat
    ftp -n -s:ftpcmd.dat ftp.company.com
    del ftpcmd.dat
    
    findstr ".htm" mls.tmp > trim.tmp
    findstr /N "^" trim.tmp > files.tmp
    
    :invalid
    set count=0
    Echo files:
    Echo.
    for /f %%a in (files.tmp) do (
    set /a count+=1
    Echo     !count!. %%a
    )
    Echo.
    set /p "choice=Enter file number: "
    if "%choice%" equ "" (Echo Invalid Choice... &pause&cls&goto :invalid)
    if %choice% lss "%count%" (Echo Invalid Choice... &pause&cls&goto :invalid)
    if %choice% lss 0 (Echo Invalid Choice... &pause&cls&goto :invalid)
    
    set count=0
    set sel=null_value
    for /f %%a in (files.tmp) do (
    set /a count+=1
    if !count!==!choice! set sel=%%a
    )
    cls
    if "%sel%"=="null_value" (
    Echo Error: File finding finding
    pause&Exit)
    cls
    Echo Selected %sel%
    pause
    cls
    
    set new=%date:~10,4%%date:~4,2%%date:~7,2%%sel:~9%
    
    cls
    echo user me@company.com> ftpcmd.dat
    echo password>> ftpcmd.dat
    echo cd %dir%>> ftpcmd.dat
    echo rename %sel% %new%>> ftpcmd.dat
    echo quit>> ftpcmd.dat
    ftp -n -s:ftpcmd.dat ftp.company.com
    del ftpcmd.dat
    del *.tmp
    

    There could be a lot of mistakes in that, but it should work. If it doesn't work, try running it in cmd as so:

    1. Start CMD
    2. Type Cmd.exe
    3. Drag and drop the batch file into the cmd window to run it from that directory

    This way you will get an error message telling you whats wrong. If you do get one, tell me what it is and I can try fixing it for you.

    Mona