Retrieve files from FTP, organize on server based on file name -


we have 2 custom indices being calculated s&p. s&p uploads 8 files (4 each index) ftp server folder can access. uploaded every evening, , retrieve them following morning. want automated because maintain 5 days on server @ time.

here example file names:

20160805_lukdgup
20160805_lukdgup_ncs
20160805_lukdgup_ncs_adj
20160805_lukdgup_ncs_cls
20160805_luksgup
20160805_luksgup_ncs
20160805_luksgup_ncs_adj
20160805_luksgup_ncs_cls

what trying download them our local server, have them automatically put folders based on file name → 20160805_lukdgup_* files go folder lukdgup\20160805\ , same luksgup files.

i've tried modifying batch files other posts, think keep making small mistakes prevent them working.

edit 20160809-0841: have been trying modify suit needs, haven't been able follow loop well:

setlocal set "basename=." pause /f "tokens=1 delims=_" %%a in ('dir /b /a-d') (    set "filename=%%a"    setlocal enabledelayedexpansion    /f "delims=" %%c in ("!basename!") if "!filename:%%c=!" equ     "!filename!" (       set "basename=!filename!"       md "!basename!"    )    move "!filename!.%%b" "!basename!"    /f "delims=" %%c in ("!basename!") (       endlocal       set "basename=%%c       pause    ) ) 

source. of course doesn't involve ftp automation, figure can part figured out filezilla or other ftp software. organizing once it's on our server piece trying solve.

do not try hack in batch file.

it's way easier implement in powershell:

$url = "ftp://user:password@ftp.example.com/remote/path/" $request = [system.net.webrequest]::create($url) $request.method = [system.net.webrequestmethods+ftp]::listdirectory $response = $request.getresponse() $reader = new-object io.streamreader $response.getresponsestream()  $listing = $reader.readtoend() $reader.close() $response.close()  $files =     ($listing -split "`r`n") |     where-object {$_ -like "*_*"}  $webclient = new-object system.net.webclient   foreach ($file in $files) {     $tokens = $file -split "_"     $localpath = (join-path $tokens[0] $tokens[1])     $localfilepath = (join-path $localpath $file)     write-host ($file + " => " + $localfilepath)      if (!(test-path -path $localpath))     {         new-item -path $localpath -itemtype directory | out-null     }      $webclient.downloadfile(($url + $file), $localfilepath) } 

Comments