WinSCP script to download files from a list (txt or csv) - ftp

I would need help to create a (manual) script to download a bunch of selected files from a designated folder.
All files are in the same folder on the FTP server. The folder holds appr. 80.000 files and I would need to download 1200 of them as set out in a list (e.g. images.txt or images.csv).
I know that it works via get command but don't know which command(s) are needed to call the items in the list, e.g.
001DD1B2-1D73-4C10-A514-BF4EA50A2103.jpg
009F64B8-1458-4238-8B84-D829912D7925.jpg
00B128EF-D6EA-4535-AE36-7AA71AD2E945.jpg
00C30DC0-10FB-4B12-B877-6C6A2E2F9194.jpg
and so on.
The script would be executed manually (no batch needed).

Based on WinSCP article Uploading a list of files, an equivalent batch file for downloading files listed in a .txt file is:
#echo off
(
echo open ftp://user:password#example.com/
echo cd /remote/source/path
echo lcd C:\local\target\path
rem Generate "get" command for each line in list.txt file
for /F %%i in (list.txt) do echo get "%%i"
echo exit
) > script.tmp
winscp.com /ini=nul /log=script.log /script=script.tmp

Related

How to save Folder Names + My own Predefined Value in a > .txt file via Batch | CMD

I'm trying to upload a bunch of .7z files to my Server where my customers can get automatic download links. Once I upload the .7z files, I need to copy/paste the last file extension in a .txt editor and parse it with my website's custom URL. It's something like this:
{.7z Files}
001187238.7z
009181905.7z
{Server/Link Folder}
https://website/s1/2291817181/918291918/
https://website/s1/8817530194/114532001/
{Download Links / Finished Process}
https://website/s1/2291817181/918291918/009181905.7z
https://website/s1/8817530194/114532001/001187238.7z
Is there any way where I can run
dir /B %userprofile%\Desktop\folder > --foldernames--.txt
and maybe combine it with a fixed string/link:
https://website/s1/2291817181/918291918/
So, that in the end I get a link + the folder name combined, and saved inside the .txt file?
Thank you!
I'm struggling to understand what you want from your description (especially, how to determine, which file should be connected to which folder), but to answer the question itself:
#echo off
(for /f "delims=" %%a in ('dir /B %userprofile%\Desktop\folder\*.7z') do (
echo https://website/s1/2291817181/918291918/%%a
)) > --foldernames--.txt

Batch script to read and extract details from a text file

I need to copy a few files and folders to their respective destinations using a Windows batch script.
All the files and folders I am supposed to copy, are kept within a folder, SOURCE.
Example:
folder: C:\X\Y\Z\SOURCE\A
file : C:\X\Y\Z\SOURCE\A.txt
file : C:\X\Y\Z\SOURCE\B.txt
folder: C:\X\Y\Z\SOURCE\ZZZ
The destination paths of all the above are provided as text file contents, destination.txt.
Content of destination.txt:
C:\FinalDestination\D\A\...
C:\FinalDestination\N\A.txt
C:\FinalDestination\C\B.txt
C:\FinalDestination\U\ZZZ\...
Where three dots at the end signifies a directory, otherwise it's a file.
What I need to do in the above scenario is:
Copy folder A from SOURCE to C:\FinalDestination\D\
Copy file A.txt from SOURCE to C:\FinalDestination\N\
Copy file B.txt from SOURCE to C:\FinalDestination\C\
Copy folder ZZZ from SOURCE to C:\FinalDestination\U\
I don't know how to do it as I am pretty new to Windows command line.
I know XCopy is a command which can work for me, xcopy source destination, but I don't know how to extract the source and destination details.
Using an unchanged destination.txt and your supplied data, the following may help:
#Echo Off
Set "sD=C:\X\Y\Z\SOURCE"
Set "sF=destination.txt"
For /F "UseBackQ Delims=" %%A In ("%sF%"
) Do For %%B In ("%%~fA.") Do echo=XCopy/IY "%sD%\%%~nxB" "%%~dpA." 2>Nul
Pause
You need only modify the content of the variables at lines 3 and 4
Note:
I have currently made it so that nothing is copied, just the commands output to your screen. If you are happy with the output remove echo= from line 7 and delete the content of line, 8

Console command to extract only a single file from archive? (7z)

I have a script to extract all the archives in a folder to folders with names of the archives, I want to modify that so it will extract only a single file from each archive (doesn't matter which one), it will behave as if there's a counter that will count 1 file and move to the next archive.
For example say we have the following files in the folder, each archive contains what's in the curly braces:
a.7z {folder{1.txt, 2.txt, 3.txt}}
b.7z {folder2{4.txt, 5.txt, 6.txt}}
c.7z {7.txt}
So running the script will result in having the following extracted (those are folders):
a{folder{1.txt}}
b{folder2{4.txt}}
c{7.txt}
Is this possible?
This is the script I currently have:
PATH %PATH%;"C:\Program Files\7-Zip";
7z x *.* -o*
You can list the files of the archive using
7z l a.7z
If all the files end with txt, you can use
7z l a.7z | find "txt"
Use this answer to get the first line:
Windows batch command(s) to read first line from text file
Then use this answer to get the file name (last word) from the line and save it in a batch variable FILE_NAME: https://superuser.com/questions/667494/batch-to-extract-a-word-from-a-string-inside-a-text-file
Then use the following command to extract the file:
7z e -i!%FILE_NAME% a.7z -o*
You can use this example to iterate all the *.7z files
Iterate all files in a directory using a 'for' loop
for /r %%i in (*.7z) do echo %%i
All the parts together:
#echo off
for %%i in (*.7z) do call :extract_file %%i
:extract_file
"c:\Program Files\7-Zip\7z.exe" l %1 > temp.txt
for /f "skip=15 tokens=1-10" %%a in (temp.txt) do (
echo %%f
"c:\Program Files\7-Zip\7z.exe" e -i!%%f %1 -o*
goto :end
)
:end
EXIT /B 0
:eof
The following works as desired. The file is "Target.zip". The top-level folder inside the ZIP file is "Target". The desired file, "Renewal.xlsm", is at a deeper level. The directory tree in the zip should be duplicated for the uncompressed file. Unlike Scripting.FileSystemObject (scrrun.dll), Windows Script Host (wshom.ocx), and Microsoft Shell Controls (shell32.dll), this solution runs asynchronously.
This will extract the single file Renewal.xlsm in Target.zip to a folder called Target.
unzip "Target.zip" "Target\NMD\2021-2022\Renewal.xlsm" -d "Target"
https://unix.stackexchange.com/a/57522/20731
unzip is installed on my corporate computer, but not my personal off-the-shelf Windows. By their own report, Unzip has vulnerabilities, so I wouldn't recommend it.
UnZip
Here's a method using 7-Zip (command-line executable 7z), which is a widely trusted tool. The original relative filepath is restored, meaning the original tree of folders leading to that file is recreated.
7z x -i!"Relative\Path\To\YourDesiredFile.ftw" "YourArchive.zip"
If you want all loose files extracted into a single directory, use e instead of x. Check out the -o switch.

Creating Bat file to execute a command on all files in folder

How to create a bat file that will execute a single command on all files with a particular extension.
I look for something like this which is there is linux for windows batch files
command *.extension
or even a way to loop through the file with extension would do.
If your command changes the filename and uses the same extension then this solution has a bug, and some files are processed more than once. There are ways around that.
#echo off
for %%a in (*.ext) do (
echo "%%a"
)

batch file to list all txt files from other directories

I am in the process of fetching all the .txt files from one directory to another (my current).
My current directory is
C:\USERS\MRAH
where i have the batch file and i have the code to fetch all .TXT files from the directory
dir E:\S_RUNS\12 month_STAR\S_2013\tst\*.txt /b >> INPUT_FILE_LIST.TXT
I am not able to fetch all the .TXT file which are in the E:\ DIREC into INPUT_FILE_LIST.TXT file on C:\USERS\MRAH
Can anyone let me know as to what should be code to fetch all the .txt file from one directory to another...
Thanks!
I'm not completely sure this will work on multiple directories but you could try it.
Cd E:\[path]
for /d %%a in (*) do (if %~xa == .txt echo %%a >> input_list.tmp)
for /f %%a in (input_list.tmp) do (copy %%a C:\USERS\MRAH)
note the batch file needs to be run from the E:[path]
also note you save it as a .tmp file to prevent it from logging itself
also instead of making an input_list file do it directly:
for /d %%a in (*) do (if %~xa == .txt copy %%a C:\users\MRAH)
Tell me if this doesn't work
Yours, Mona
Assume your current working directory is c:\testDir and you wanna copy all txt files from c:\source to d:\dest then use following content in a batch file
copy c:\source*.txt d:\dest

Resources