Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4251
  • Last Modified:

get file using WINSCP WITH date appended

Here is the code I am trying to run.
@echo off

@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(

Set DayW=%%A
Set Day=%%B
Set Month=%%C
Set Year=%%D

Set All=%%D%%B%%C
)

cd "C:\Program Files (x86)\WinSCP"
WinSCP.com /command "open Sharefile" "option confirm off" "cd /test/" "get CCC_DDD_%All% C:\CCC_DDD_%All%"

PAUSE

EXIT

I am trying to pull a file with WINSCP and Sharefile is my Session name.. I am trying to pull down a file named CCC_DDD_20120816.csv

The error message I am getting is Can't get attributes of file 'CCC_DDD_20120816'
File or folder 'CCC_DDD_20120816' does not exist.

WHen i run the following batch file with a specific date it pulls down the file .

cd "C:\Program Files (x86)\WinSCP"
WinSCP.com /command "open Sharefile" "option confirm off" "cd /test/" "get CCC_DDD_20120730.csv C:\CCC_DDD_20120730.csv"

pause
exit
0
Shalver
Asked:
Shalver
  • 7
  • 5
1 Solution
 
oBdACommented:
Assuming your date extraction is correct (from your output, it looks like Day and Month are reversed in the "For" loop, but it works because the All is set without using these), the issue is that you forgot to add the file extension (".csv") to the fil enames in your script:
@echo off
For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do (
  Set DayW=%%A
  Set Day=%%B
  Set Month=%%C
  Set Year=%%D
  Set All=%%D%%B%%C
)
cd "C:\Program Files (x86)\WinSCP"
WinSCP.com /command "open Sharefile" "option confirm off" "cd /test/" "get CCC_DDD_%All%.csv C:\CCC_DDD_%All%.csv"

PAUSE
EXIT

Open in new window

0
 
ShalverAuthor Commented:
When i added the file extension of .csv it still gave the same message

The error message I am getting is Can't get attributes of file 'CCC_DDD_20120816.csv'
File or folder 'CCC_DDD_20120816.csv' does not exist
0
 
oBdACommented:
Have you verified that the file actually exists?
Is the host maybe cAsE sEnSiTiVe, and your download name doesn't match the exact case of the file name on the host?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ShalverAuthor Commented:
Yes,
The file does exist and the file name is exactly as it is on the FTP.
0
 
ShalverAuthor Commented:
It works if I manually put in a date cd "C:\Program Files (x86)\WinSCP"
WinSCP.com /command "open Sharefile" "option confirm off" "cd /test/" "get CCC_DDD_20120730.csv C:\CCC_DDD_20120730.csv"
0
 
oBdACommented:
Well, does it work if you manually enter name for the today's file? Your manual examples only show the success of an older file.
The file name in the error output matches exactly what should be there.
0
 
ShalverAuthor Commented:
How can I run thsi script to display yesterday's date rather then todays?
0
 
oBdACommented:
Use this to retrieve today's or yesterday's date, then put your time stamp together from the variables provided as you need them:
@echo off
setlocal enabledelayedexpansion
set /a Line=0
for /f "tokens=1-9" %%a in ('wmic Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
  set /a Line += 1
  if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
  if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
set /a i = 1&for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (set Month[!i!]=%%a&set /a i+=1)
set /a Leap = Year %% 4&if !Leap!==0 (set Month[2]=29)
set /a pYear = Year
set /a pMonth = Month
set /a pDay = Day - 1
if !pDay!==0 (
  set /a pMonth -= 1
  if !pMonth!==0 (
    set /a pYear -= 1
	set /a pMonth = 12
  )
)
if !pDay!==0 (set pDay=!Month[%pMonth%]!)
for %%a in (Month Day Hour Minute Second pMonth pDay) do (if !%%a! LSS 10 set %%a=0!%%a!)
REM *** At this point, the variables Day, DayOfWeek, Hour, Minute, Month, Quarter, Second, WeekInMonth, and Year are set.
REM *** Month, Day, Hour, Minute, Second have leading zeroes if less than 10.
REM *** In addition, the following variables for the previous day are set: pDay, pMonth, pYear
echo Previous day: %pMonth%/%pDay%/%pYear%

Open in new window

0
 
oBdACommented:
I'll be offline for the next hours, so if I'm not responding, it doesn't mean I have forgotten about you ...
0
 
ShalverAuthor Commented:
So I took your exact script and just took off the last line (line 26)

and then added cd "C:\Program Files (x86)\WinSCP"
WinSCP.com /command "open Sharefile" "option confirm off" "cd /test/" "get CCC_DDD_%pYear%%pMonth%%pDay%.csv c:\CCC_DDD_%pYear%%pMonth%%pDay%"

ANd now I get
Starting the session....
reading remote directory.
Session started...
Can't get attribute of file 'CCC_DDD_20120815.csv'
Invalid access to memory.

pause
exit
0
 
ShalverAuthor Commented:
Thanks for helping me out.... I needed to add (.csv) to the end of my script and now it works fine :)
0
 
ShalverAuthor Commented:
Can you explain what is going on in the script that you wrote to get the previous day so Ican understand it better
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now