I'm trying to create a combination of batch files, with ftp commands to get and delete a specific number of files from a FTP directory.
Now I'm stuck and I get all the files but ofter, when there are more than 250 files in the directory, the batch stops and it's not possible to continue.
This is my actual situation, I have these 3 files:
FILE 01_FTP_GetFileList.bat
cd C:\folder
ftp -s:02_FTP_GetFileList.txt
C:\folder\03_FTP_MoveFiles.bat
FILE 02_FTP_GetFileList.txt
open xxx.xxx.xxx.xxx
user
password
hash
prompt off
cd folder
lcd folder
ls *.gz filelist.txt
bye
FILE 03_FTP_MoveFiles.bat
#echo off
setlocal enableextensions
setlocal enabledelayedexpansion
echo open xxx.xxx.xxx.xxx>>myscript
echo user user password>>myscript
echo prompt n>>myscript
echo ascii>>myscript
echo cd folder>>myscript
echo lcd C:\folder>>myscript
for /F "usebackq tokens=1,2* delims=," %%G IN ("C:\folder\filelist.txt") DO echo mget %%G>>myscript
for /F "usebackq tokens=1,2* delims=," %%G IN ("C:\folder\filelist.txt") DO echo mdelete %%G>>myscript
echo bye>>myscript
ftp -n -s:myscript
del filelist.txt
del myscript
To resume, the first file recall the 2nd and the 3rd and, as you can see in the code, I get and delete all the *.gz files in a specific directory after listing the files in that directory (this to avoid to delete file added after the copy).
Now I would like to copy only the first file of the list or... to list only 100 files in the folder, is the same by my point of view.
Thanks in advance!!
I do not see any counter variable in the post. The code below uses FILE_COUNT as the counter. It is incremented by the SET /A FILE_COUNTER+=1 statement. The FILE_COUNT is tested for being less than 100. See SET /?.
SETLOCAL ENABLEDELAYEDEXPANSION
SET MAX_FILES=100
SET /S FILE_COUNT=0
FOR /F "usebackq tokens=1,2 delims=," %%a IN (`TYPE "C:\folder\filelist.txt"`) DO (
IF !FILE_COUNT! LSS %MAX_FILES% (
echo mget %%a>>myscript
echo mdelete %%a>>myscript
)
SET /A FILE_COUNT+=1
)
Related
I am making a file converter with batch using FFMPEG.
I have encountered a problem when trying to subtract the name of a file from the complete link to the file to get the directory of the folder it is in so after the file is converted it can be put into that directory.
Can someone advise me on how I could subtract the string from the filename variable from the string in the directory
My code:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set filetype=.flac
for /R %%a in ("*%filetype%*") do (
set directory=%%a
set filename=%%~na%filetype%
set convdir=!directory:%filename%=!
echo !convdir!
pause
ffmpeg -i "%%a" "convdir%%~na.wav"
echo Converted %%a
)
echo Convertion Done!
pause
Here's a quick, untested, example of how I'd suggest your task is performed:
#Echo Off
SetLocal EnableExtensions
Set "TreeRoot=."
Set "FileGlob=*.flac"
Set "ConvExtn=.wav"
For /F "Delims=" %%G In (
'"(Set PATHEXT=) & "%__APPDIR__%where.exe" /F /R "%TreeBase%" "%FileGlob%" 2> NUL"'
) Do (
Echo Converting %%~G to %%~nG%ConvExtn%
"ffmpeg.exe" -i %%G "%%~dpnG%ConvExtn%"
If Not ErrorLevel 1 Echo Conversion completed.
)
"%__APPDIR__%timeout.exe" /T 5 1> NUL
Simply adjust the variable values on lines 3 to 5 as necessary. I've used . as the tree root as that is the current directory. Please note however, that your code does not define the current directory, so if this is supposed to be rooted at the location of the batch file itself, you should probably change that to %~dp0..
I have a folder containing many files named as such: JBMA_23456.docx, JMRI_21456.docx, CM_22554.docx, QUA_11224.docx. How do I create a sub-folder for each file bearing the same name as the file but without the .docx file extension? Additionally, I want to store the filename only as variables.
For example, I need to create a sub-folder named JBMA_23456 from the document
JBMA_23456.docx. Can anyone point me in teh right direction?
#echo off
for %%A in (*.docx) do if not exist "%%~nA" md "%%~nA"
This creates a folder with the same name as each .docx file.
View modifiers in for /? or in call /?. The n modifier is the name.
Path modifiers:
dpnx is drive, path, name and extension.
Here's an example batch file which attempts to perform the tasks as laid out in your question:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "BaseDir=C:\Users\Compo\Desktop\test"
Set "FileExt=.docx"
For /F "Delims==" %%A In ('Set var[ 2^>Nul') Do Set "%%A="
Set "i=0"
For /F "Delims=" %%A In ('Where "%BaseDir%":*%FileExt% 2^>Nul') Do (Set /A i+=1
Call Set "var[%%i%%]=%%~nA"
If Exist "%%~dpA%%~nA\" (Echo Sub-Folder %%~nA already exists in %BaseDir%
) Else (Set /P "=Creating sub-folder %%~nA in %BaseDir%"<Nul
MD "%%~dpA%%~nA">Nul 2>&1 && (Echo= was successful) || Echo= failed))
Set var[ 2>Nul
Pause
In order to use it, you would first ensure that the directory holding your files, (excluding any trailing backslash), is placed between the = and " on line 4, and the single file extension, (including the leading period, .), similarly on line 5.
It is not entirely clear what you are asking for...
Anyway, here is the code i made
echo off
chcp 65001
cls
for /f "usebackq delims=." %%0 in (`dir /b "*.docx"`) do (
set filename=%%0
md %filename%
)
cmd /k
It creates a new folder for every file. Feel free to ask if this was not what you expected
I am creating a batch file to open remote Computer Management console by taking User ID as input and computer name from 2nd column from file data.csv. it works fine on first attempt. When it goes back to :start label. and ask for other input. it gives error. System cannot find file ./data.csv
My code is
:start
set /p Input="Enter User-ID:"
for /f "usebackq tokens=1-4 delims=," %%a in (".\data.csv") do (
if %input% ==%%a ("cmd /c Start /B /wait compmgmt.msc –a /computer=%%b")
)
cls
GOTO start
Good practice to use %~dp0 for paths in batch files (instead of relative paths like .) that way if the current working folder changes the file will always be located.
So change to %~dp0data.csv
:start
set /p Input="Enter User-ID:"
PUSHD
for /f "usebackq tokens=1-4 delims=," %%a in (".\data.csv") do (
if %input% ==%%a ("cmd /c Start /B /wait compmgmt.msc –a /computer=%%b")
)
POPD
cls
GOTO start
should restore sanity, pushing the directory then restoring it before the next cycle.
Im making a small script but i reached a head scratch
I want the batch file to look for example.exe or Folder name in all drives then cd to it and create a txt file inside it? is that even possible :P?
cd /d D:
dir example.exe /s /p
lets say its found and the dir is D:/Example.exe
so i want the batch to do this,
if example.exe is found cd to it directory then
REM. >> "D:/logs.txt
is that possible? –
what do i put after "if exist" for the batch file to automatically switch to the found file directory
#echo on
cd /d D:
dir example.exe /s /p if exist (whether its in D:/Folder/folder or D:/Folder go to directory)
echo >test.txt
pause
Finally Solution by #Stephan best working answer
#echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('wmic logicaldisk where "size>0" get caption^|find ":"') do (
for /f "delims=" %%A in ('dir /s /b %%a:\example.exe 2^>nul') do (
ECHO break>"%%~dpAtest.txt"
)
)
To catch the output of a command, use for.
dir /p does not make sense here (it's to pause if the output is longer than the screen). You want /b (bare format; filename only / drive/path/filename when used with /s).
%%~dpA gives drive:\path\ only.
break>filename to create an empty file (REM. does also work, but I prefer break)
#echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('wmic logicaldisk where "size>0" get caption^|find ":"') do (
for /f "delims=" %%A in ('dir /s /b %%a:\example.exe 2^>nul') do (
ECHO break>"%%~dpAtest.txt"
)
)
this puts an empty test.txt to every folder in every available drive where there is an example.exe. If there already should be a test.txt, it gets overwritten by an empty file.
NOTE: this only echoes the break command to the screen (for security reason). If the output is like you want it, remove the ECHO.
Updated answer, since I got your question after this update:
In order to process all the files you found:
FOR /F "delims=#" %%f IN ('dir example.exe /s /b ') DO (
echo example.exe was found at path %%~pf
)
Now you can modify your ECHO statement:
ECHO some text > C:\logs.txt
If you want to append to the file instead of replacing, you have to use two brackets:
ECHO some text >> C:\logs.txt
You can enter a path after > . You don't need to CD before.
You can also use the variable for CD:
cd %%~pf
If you want some other part of %%f, you can do FOR /? on your command line or see the reference
Batch file (command line) to get target path of Internet Shortcut (.url) in the "URL:" field under "Web Document" tab
I need the get the target from all shortcuts in a folder (desktop) and their respective destinations to help people at my computer repair shop defend against customers/clients re-infecting their computers by going to these web sites.
I have the code already to display (below) but I need to display the destination as well when it finds one (a .url shortcut file)
dir /b "%userprofile%\Desktop*.url"
-Thanks, -G
I think i understand your question, here is a batch file that will loop through the links in a given folder and then echo each of the destination URLs
Batch file:
echo off
setlocal enableextensions enabledelayedexpansion
pushd %1
for %%F in (%1\*.url) do (
echo %%~dpnxF
call :findurl "%%~dpnxF"
)
popd
goto end
:findurl inputfile
set url=
for /f "tokens=2 delims==" %%i in ('findstr URL %1') do set url=%%i
echo %url%
echo -----
:end
Usage:
MyBatchFile.bat C:\users\Username\Desktop
Expected Output:
d:\Users\Martyn\Desktop\test\link - Copy.URL
http://google.co.uk
-----
d:\Users\Martyn\Desktop\test\link.URL
http://stackoverflow.com/questions/17687358/batch-file-command-line-to-get-target-path-of-internet-s
hortcut-url-in-the
-----
[edit] Just a quick explanation on whats going on, .url files are basically just text files, open one in notepad to see. the URL is on the line URL=http://foo.com
this batch just reads each .url file in a directory and then echos everything on the URL line after "URL="
Hope that's what you meant!
Martyn
This format will also work.
#echo off
Setlocal Enableextensions
Setlocal Enabledelayedexpansion
for /r %%X in (*.url) do (
set shortcut="%%X"
echo SHORTCUT: !shortcut!
for /f "tokens=2 delims==" %%i in ('findstr URL !shortcut!') do (
set url=%%i
echo.
echo URL PATH: !url!
)
echo ----------------------------------------------------------------
echo.
)
:end