Managing filename variable between plink and pscp on batch file

I have the next plink command that returns a file name that later on i need to download locally via pscp

i can get successfully the file name via plink

and i can download the file over pscp if i have the file name

now i need to move the file name retrieved over plink (now to a text file) to be written in the command ill use with pscp

here is the first command working in plink:

link  -l username -pw password machine"cd /route/; ls -alrt C*.TXT | tail -1 | awk '{print $NF}'" >> filename_here.txt 2>&1

i need to use this output (now in a txt file called filename_here) for my pscp command
LVL 1
celticianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
How to handle this correctly depends on the exact contents of filename_here.txt.
If it's just a file name in a single line, the following command will put it into the variable "PlinkFilename":
for /f "delims=" %%a in ('type "filename_here.txt"') do set PlinkFilename=%%a

Open in new window

If the file contains more output, please provide a sample.
0
celticianAuthor Commented:
it contains a short string with the name

ABCD.TXT

simply that, just only a short string output like that.

if i can handle directly the output without the need of writing it to the txt file it would be even better, i just used this txt file to test the plink command was working fine, but i really dont need it at all.
0
woolmilkporcCommented:
plink  -l username -pw password machine"cd /route/; ls -alrt C*.TXT | tail -1 | awk '{print $NF}'" >> filename_here.txt 2>&1

set /p VAR=<filename_here.txt

pscp -P portNumber -pw Password UserName@maquine:/route/%VAR% "c:\destinationFolder"
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

oBdACommented:
It could work, but it might be tricky because of the quotes. Try it like this:
for /f "delims=" %%a in ('link -l username -pw password machine"cd /route/; ls -alrt C*.TXT ^| tail -1 ^| awk ^'{print $NF}^'"') do set PlinkFilename=%%a

Open in new window

0
celticianAuthor Commented:
@woolmilkpork, not working that solution, i noticed that it takes a while until the txt file is written in, about two seconds, maybe the second sentence tries to read the content of the file when its still empty??

im trying now the second option, id like not to use the txt file, just directly into a variable if possible, or deleting the txt file aftwards to prevent it from reading the next days from the same txt file and causing conflict :)
0
celticianAuthor Commented:
No luck with the @oBdA proposal either, id like to see whats going on, tried to put a pause at the end of each sentence, but didn't work
0
oBdACommented:
Try it without the escapes:
for /f "delims=" %%a in ('link -l username -pw password machine"cd /route/; ls -alrt C*.TXT | tail -1 | awk '{print $NF}'"') do set PlinkFilename=%%a

Open in new window

0
celticianAuthor Commented:
PS when executing the solucion frmo woolmilkpor i noticed that in the generated txt file its being entered a carriage return/jump line character, this migh affect the command given to pscp as it would be a different filename?
0
celticianAuthor Commented:
Not working without the escapes either, this is actually what im writing in the second command (pscp):

pscp -P 22 -pw Password User@machine:/route/%%a "c:\downloads"
0
oBdACommented:
The second command will need to reference the PlinkFilename variable; %%a is only valid within the for loop:
for /f "delims=" %%a in ('link -l username -pw password machine"cd /route/; ls -alrt C*.TXT | tail -1 | awk '{print $NF}'"') do set PlinkFilename=%%a
echo Downloading '%PlinkFilename%' ...
pscp -P 22 -pw Password User@machine:/route/%PlinkFilename% "c:\downloads"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
celticianAuthor Commented:
Not working yet, this is the full script:

for /f "delims=" %%a in ('plink -P 22 -l User -pw Password Machine "cd /route; ls -alrt C*.TXT | tail -1 | awk '{print $NF}'"') do set PlinkFilename=%%a
echo Downloading '%PlinkFilename%' ...
pscp -P 22 -pw Password User@Machine:/route/%PlinkFilename% "c:\downloads" 

Open in new window


It would be interesting getting some at least error output or finding out whats going on here as when i double click the batch it opens up a command line windows that closes itself almost inmediately.
0
oBdACommented:
Open a command prompt, enter "cd /d P:\ath\to\the\script", then start the script from this open command prompt instead of double clicking it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.