I have a directory with a list of files, I would like that my batch finds the most recently added file and then copies the content in another file in another directory. So far I did like this:
for /f "delims=" %%x in ('dir /od /a-d /b c:\dir1\*.log') do type %%X >> c\dir2\all.log
The problem is that in this way it outputs for example
ex150422.log 1>>c:\dir2.log
but it doesn't copy the content..
What am I doing wrong?
Thanks!
are you shure it can find the file? add the directory name like you did in dir-command
for /f "delims=" %%x in ('dir /od /a-d /b c:\dir1\*.log') do (
type c:\dir1\%%x>>c:\dir2\all.log
)
Related
I want to loop through a folder and its subfolders, get all .jpg files and run myapp.exe on the files where I rename the currently found file's extension. myapp.exe takes the full file path as input and a renamed file as output, like so:
myapp.exe C:\images\original\210_551_210-768--RJF3823klzw.jpg -o C:\images\original\210_551_210-768--RJF3823klzw.gif
I now have this to recursively print the full path of *.jpg files.
for /f "delims=" %%i in ('dir /a-d/b/s *.jpg') do echo "%%i" "%%~ni.gif"
However, this prints only the filename for the second parameter, whereas I want to include the path of the found file.
How can I include the path of the output file, the second parameter for myapp.exe?
Pseudocode:
for /f "delims=" %%i in ('dir /a-d/b/s *.jpg') do myapp.exe "%%i" "<FULLPATH><FILNAME>.gif"
If myapp.exe actually converts the file instead of simply changing the extension then you can use metavariable expansion, (see For /? for examples):
For /F "Delims=" %%A In ('Dir /B/S/A-D *.jpg') Do myapp.exe "%%A" "%%~dpnA.gif"
If you're only replacing the extension the you can just use the rename command:
For /F "Delims=" %%A In ('Dir /B/S/A-D *.jpg') Do Ren "%%A" "%%~nA.gif"
In either case you could also change your for loop to:
For /R %%A In (*.jpg)
Looking for a way to run a command, but insert the path/filenames of all mp4 files from the working directory (where the script is run) to the command.
vlc.exe c:\path\filename1.mp4 c:\path\filename2.mp4
I have the following code, but the "%%~A" is only inserting one path/filename at a time instead of adding every path filename from the folder.
set dir=C:\Users\Administrator\Desktop\1
for /f "delims=" %%A in ('dir /b "%dir%\*.*"') do ("C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "%%~A" --sout=#transcode{vcodec=mp2v,vb=800,acodec=mpga,ab=128,channels=2,samplerate=44100}:http{mux=ts,dst=:8080/abc} --sout-keep --loop)
An elementary solution: first try
set "dir=C:\Users\Administrator\Desktop\1"
dir /b /a:-d "%dir%\*.*"
The same result (filenames only) as in:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%%~A"
So add full path as follows:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%dir%\%%~A"
Eventually, replace #echo "%dir%\%%~A" with your command...
How to copy the recently modified file from source to destination.
Constraint: All the files starting with same name in the source folder.
Example: Source - C:\Source and is having files like sys1239_5241.KNL, sys1234_8741.KNL
So how to copy latest modified files in batch file and modification will happen in minutes not in dates.
for /f "delims=" %%i in ('dir /b /a-d /od "C:\Source\*.KNL"') do set "LatestModifiedFile=%%~i"
echo copy "C:\Source\%LatestModifiedFile%" "X:\destination\path"
..remove echo if it looks good.
The following works well.
for /f "delims=" %%i in ('dir /b /a-d /od "C:\Teste_1\*"') do set "LatestModifiedFile=%%~i"
copy "C:\Teste_1\%LatestModifiedFile%" "C:\Teste_2\"
pause
This question might have a simple solution, but I just cannot find it.
Let's say I'm using the DIR command to produce a list of .txt files in the folder "E:\Documents". How can I make sure that only the .txt files one level below "E:\Documents" are listed (i.e. in a direct subfolder) and not the files in "E:\Documents" itself. "E:\Documents\\" or "E:\Documents\*\" doesn't seem to do the trick.
Thanks in advance!
FOR /f "delims=" %%i IN ('dir /b/ad') DO IF EXIST ".\%%i\*.txt" DIR ".\%%i\*.txt"
That's if you want the output in DIR format - with headers
FOR /f "delims=" %%i IN ('dir /b /ad') DO IF EXIST ".\%%i\*.txt" (
FOR /f "delims=" %%q IN ('DIR /b ".\%%i\*.txt" ') DO ECHO ".\%%i\%%q"
)
if you want just the filenames.
This can be achieved without dir:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~ff
If you need not only the file names, but also detail information about the files, add the respective qualifiers, e.g.:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~azff
a → attributes of the file
t → timestamp of the file
z → size of the file
See help for for details.
I have this batch:
for /f %%a IN ('dir /b *.pdf') do call convert.exe %%a
This gets every pdf file thats in the same folder as convert.exe. I want to be able to say where the PDF resides. What do I have to change?
Thanks!
If the directory name can be hardcoded, then it will be
for /f %%a IN ('dir /b /s "Disk:\Your\Directory\Name\*.pdf"') do call convert.exe %%a
Note that this will also return all .pdf files in subdirectories of Disk:\Your\Directory\Name.