Windows batch: set up reference folder variable and loop files

Hello experts,
I have the following batch file to export files.
I would like to
-Set reference folder instead of using cd command
-Add a for in loop in order to: loop the various files in a directory instead of reporting file name.
ie@filename.csv
If you have questions, please contact me.
Thank you.

@echo off

rem Set curl path.
set CURL="curl/curl"

cd "C:\Temp"

rem Set site url.
set SITE_URL=

rem Set login.
set LOGIN=

rem Set password.
set PASSWORD=

rem Set action url.
set ACTION_URL=%SITE_URL%/sendFile/51

rem Set input file path and name.
set PARAM_PROXY=

set FILE="file=@filename.csv"

rem Import the file.
curl -v -k %ACTION_URL% -u %LOGIN%:%PASSWORD% -F %FILE%

pause

Open in new window

LVL 1
LD16Asked:
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.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
1) Set reference folder instead of using cd command

As I mentioned in your previous question. Use --output $dir/$file to direct your output to any path name required.

So in your case your reference folder will become $dir above.

2) Add a for in loop in order to: loop the various files in a directory instead of reporting file name.

You'll first have to ingest a directory list of files with all extensions of interest, which seems to be .csv in your case.

Then process each file.

This becomes a bit complex, because when you use curl, you're pulling down a file from an external source, so you can never know all the files at the external source, unless you run a remote dir command.

You can do this by running some ssh equivalent (like Putty) to generate an external file list.
LD16Author Commented:
Could you please make a proposal based on the question posted?
LD16Author Commented:
Something like this?
for file in /this/is/my/path/*
do
    curl -X POST -T "/this/is/my/path/$file" https://whatever;
done; # file

Open in new window

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Bill PrewIT / Software Engineering ConsultantCommented:
Give this a try.

@echo off
setlocal

rem Set base folder to upload files from
set BaseDir=c:\temp

rem Set curl path.
set CURL="curl/curl"

rem Set site url.
set SITE_URL=

rem Set login.
set LOGIN=

rem Set password.
set PASSWORD=

rem Set action url.
set ACTION_URL=%SITE_URL%/sendFile/51

rem Set input file path and name.
set PARAM_PROXY=

rem Process all CSV files in base folder
for %%F in ("%BaseDir%\*.csv") do (
    rem Import the file.
    "%curl%" -v -k %ACTION_URL% -u %LOGIN%:%PASSWORD% -F "file=@%%~F"
)

pause

Open in new window


»bp

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
LD16Author Commented:
Thank you Bill. Possible to add controls:
Log file with the following actions:
-If basedir is empty exit and report :YYYYMMDD-HHMSS: no files to upload
Else: YYYYMMDD-HHMSS: filename has been sent.
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, give this a try:

@echo off
setlocal EnableDelayedExpansion

rem Set log file path
set LogFile=c:\temp\logfile.txt

rem Set base folder to upload files from
set BaseDir=c:\temp

rem Set curl path.
set CURL="curl/curl"

rem Set site url.
set SITE_URL=

rem Set login.
set LOGIN=

rem Set password.
set PASSWORD=

rem Set action url.
set ACTION_URL=%SITE_URL%/sendFile/51

rem Set input file path and name.
set PARAM_PROXY=

if not exist "%BaseDir%" (
    call :GetTimeStamp
    echo !TimeStamp! : Base folder does not exist "%BaseDir%"
    exit /b
)

if not exist "%BaseDir%\*.csv" (
    call :GetTimeStamp
    echo !TimeStamp! : No files to upload.
    exit /b
)

rem Process all CSV files in base folder
for %%F in ("%BaseDir%\*.csv") do (
    rem Import the file.
    "%curl%" -v -k %ACTION_URL% -u %LOGIN%:%PASSWORD% -F "file=@%%~F"
    call :GetTimeStamp
    echo !TimeStamp! : File has been sent "%%~F"
)

pause

exit /b

:GetTimeStamp
    REM Get current date/time
    set TimeStamp=
    for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
        if not defined TimeStamp (
            set TimeStamp=%%A
        )
    )

    REM Format current date/time as YYYYMMDD_hhmmss
    set TimeStamp=!TimeStamp:~0,8!_!TimeStamp:~8,6!

    exit /b

Open in new window


»bp
LD16Author Commented:
Thank you Bill.
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome!


»bp
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.