Window shell - list of file as a command input - windows

In Unix, I can provide a command with a list of files by doing that:
mycommand folder/*
The argc will then be equal to the number of files in the directory and argv to the name of each files in the directory.
However, this doesn't seem to be the same on Windows. Is there a way to emulate this without listing all the files of the folder as argument to the command?
Thanks.

Windows command prompt does not natively support wildcard expansion.
If "myprogram" is an application build with Visual C++ and you have control over how it is built, you can add support for wildcards to the application itself, as described in MSDN article Expanding Wildcard Arguments

From here:
To delete every .bak file in every subfolder starting at C:\temp
C:\>FOR /R C:\temp\ %%G IN (*.bak) DO del %%G
Also take a look on FORFILES.

Related

Find a program and run it through command prompt

I want to be able to locate where the exe of a (third-party) program I remember from only its name. (say photoshop.exe) Is that possible?
Like you can call taskmrg.exe
try this to find:
dir /s c:\photoshop.exe
..and also to run:
for /f "delims=" %a in ('dir /b /a-d /s "c:\photoshop.exe"') do "%~fa"
Yes you can do that of course, but it involves searching for the file recursively in every folder of your hard drive partition(s).
Another way is to add the folder containing the .exe you want to execute in the PATH environment variable but it's not recommended to add every program you own to the PATH. (It's meant to remain quite small in size).
Here's the syntax of FIND command in dos.
http://www.computerhope.com/findhlp.htm
taskmgr.exe can be executed from any path at the command prompt, because its location falls within the directories listed in the %PATH% variable. You can potentially add any additional folders you would like to this variable to make any applications within follow the same behavior. Caveat: Some applications do not run properly unless started from within their home directory.
Editing the %PATH% variable via the GUI varies between versions of Windows, or you can edit it from the command prompt.

Win 7: CMD batch file for creating directories based on filenames

I'm working on a CMD line batch file in a Win7 environment that will create directories based upon the filenames listed in a directory.
I am using this code, but the output created is partial and incomplete with only
setlocal enabledelayedexpansion
for /r %%i in (*.wav) do (
set filename1=%%i
set folder1=!filename1:~4,10!
mkdir !folder1!
)
pause
I have this script saved as a CMD file in text format in the source directory, on a local harddrive, though it is in a subdirectory.
The directory output is partial and broken, with garbled output and the numbers of directories created does not match the number of files, and the created directories seem to nest. I've researched this and can't seem to find a definitive answer.
It's not entirely clear what it is you are trying to accomplish. Are you trying to create a directory within the same directory containing the wav file, just without the .wav extension? If so, you're missing some quotation marks and you're stripping the wrong end of the filename off. If that's what you are trying to accomplish, it can actually be done with a single command, no batch script needed. Type this at the command prompt:
for /r %I in (*.wav) do mkdir "%~pnI"
Of course, if you still want it in a batch script, use %%I and %%~pnI with double percents instead of single. See the last couple of pages of help for for an explanation of how %%~pnI works.

Window Cmd Prompt - Move file with special character

i got a file by the filename
[vvv]_PHØDE:GREAKER_-_01_[720p][10bit][z11].mkv
how do i move the file with the move command at command prompt to make it this way
move "*GREAKER*.mkv" "PHODE_GREAKER_-_01_[720p][10bit][z11].mkv"
i want the second * to be replace at the destination as _-01[720p][10bit][z11] after the breaker.
In linux we can use regex pattern like (*.?) something like this , but how do i move it at window.
to take away this [vvv]_PHØDE:and make it as the string "PHODE"
On windows, MOVE is mainly used to move a file from one folder to another. It can only rename the file if the MOVE command is operating on a single file. If you are using wildcards in your source file then you should use REN (or RENAME) instead.
But, you have another problem that is more problematic. You have the : character in your file name, which is not valid for Windows. This may be impossible to fix with standard Windows commands and utilities. Perhaps one of the following SuperUser links can help:
How to batch rename files copied from OSX to Windows with ':' in filenames?
How to force Windows XP to rename a file with a special character?
Files with illegal filenames
Try this:
setlocal EnableDelayedExpansion
for %%f in (*GREAKER*.mkv) do (
set name=%%~f
ren "%%~f" "PHODE_!name:~12!"
)
endlocal

Batch File Works in Windows Vista; Results in "File Not Found" on Windows 7

The following batch file, meant to parse a directory and send each file to the specified program, works in Windows Vista x64:
#ECHO OFF
FOR /F "tokens=1,2 delims=." %%A IN ('dir /b /on *.mts') DO (
"C:\Program Files (x86)\DGAVCDecode\DGAVCIndex.exe" -i %%A.%%B -o %%~nA.dga -f 2 -a -e
)
In Windows 7 x64, cmd returns "File Not Found"—both as a normal user and Administrator. What's going on?
You might want to use %PROGRAMFILES% instead of hard coding "c:\program files" into your batch file. For 64bit windows, there's also %PROGRAMFILES(x86)% which points to the 32bit program files directory.
I see the following problems in your code:
Looks like you use tokens=1,2 delims=. to split the file name by dot into the base name and extension and then join them back as %%A.%%B. This won't work with file names that contain dots, because it captures only the first two tokens from the file name. For example, given the file name foo.bar.mts, %%A.%%B will expand to foo.bar.
Moreover, this split/join isn't actually needed. If you use the loop without any parsing options, the file name is stored in the loop variable so that you can simply use that variable instead of %%A.%%B.
You need to enclose the file names passed to DGAVCIndex.exe in quotes, in case they contain spaces.
Also, I second Larry's suggestion to use %PROGRAMFILES(x86)% instead of C:\Program Files (x86) — it never hurts to use predefined environment variables instead of hard-coding standard system paths.
So, your code should look like this:
#echo off
for %%f in (*.mts) do (
"%ProgramFiles(X86)%\DGAVCDecode\DGAVCIndex.exe" -i "%%~f" -o "%%~nf.dga" -f 2 -a -e
)
This might seem obvious, but does DGAVCIndex.exe exist on the Win7 machine at the specified location?
Are you sure the folder names are correct with respect to the 32 bit and 64 bit versions of Windows 7. Did you check whether your batch file exists in the location that you have mentioned in the bat file.
Explorer mimics some directories like "C:\Program Files" and "C:\Users". When you use a localized windows 7, the directories have still the same name, but Explorer displays something localized like "C:\Programme" or "C:\Bemutzer".
Dont trust Explorer use the command line for copying files on a location you want to specify.

Copy a directory tree to a single directory at a command line

Anyone know of a command line utility (or one that can run as a command line) that will collect all the .jpg files in a directory tree to a single folder, only copying files that change?
I started with Renamer, which is great for renaming files in their current directories, but fell short when I tried to mangle the path. This is probably because I don't know Renamer that well. I ended up creating a text file directory dump, then using a REGEX find / replace to create a batch file, but this is hardly efficient nor automated.
The REGEX:
(G:\DIR\DIR\)([0-9]+\)([0-9]+\)([0-9]+\)([0-9]+\)(p[0-9]+.jpg)
changed this
G:\DIR\DIR\00\00\00\00\p0000000000.jpg
to this
G:\DIR\DIR\p0000000000.jpg
(copy \1\2\3\4\5\6 \1\6) in the batch file.
I need to run the whole thing as a scheduled task without a real person logging in. Not really looking for a Zip file because I don't want to disturb the system processor, plus most of the files will not change from day to day. This is more of a file sync.
In a Windows command line you can do this:
for /R A %i IN (*.jpg) DO xcopy %i B /M /Y
Where A is the source directory and B is the destination directory. You need to have command extensions enabled, which I believe is the default.
A couple of notes from the comments:
If any of your paths could have spaces in you will need to add quotes around the second %i. This prevents the string being interpreted by the xcopy command as two separate parameters. You may need to do the same around A and B paths. Like this:
for /R "A" %%i IN (*.jpg) DO xcopy "%%i" "B" /M /Y
If you are putting this inside a .bat or .cmd file you will need to double the percentage like this.
for /R A %%i IN (*.jpg) DO xcopy %%i B /M /Y
The /M option on xcopy will only copy files with the Archive bit set and then unset this bit. This prevents the files being copied twice. If you have other processes that also alter this bit it may cause issues. It is also possible to use the /D option which compares the file's last modified time with that in the destination and only copies newer files.
I'm guessing you're on Windows from the path format.
I've not read the whole thing, but http://www.infionline.net/~wtnewton/batch/batguide.html#6a might help you.
The same page has dizzy.bat, (http://www.infionline.net/~wtnewton/batch/dizzy.bat) which should be trivial to edit to do what you want.
In a Unix environment I would use find or rsync (and maybe some features of the shell). Cygwin and MinGW come with find, maybe with rsync. You can also probably get a standalone port of find for Windows somewhere.
If the SOURCE shell variable is the directory containing subdirectories with files to copy, and the DEST shell variable is the directory to copy them to:
find $SOURCE -name \*.jpg -exec cp --update \{\} $DEST/ \;
find is by nature recursive. "-name \*.jpg" selects files that match that pattern. You can add additional conditions with -and. The --update option to the cp command (or -u) only bothers copying the file if changed or not yet copied. There are other options to cp that might be useful too.
If $SOURCE is the same as $DEST as in your DIR/DIR/ example, then find will also find the destination files (already copied), though this will be ok, cp will recognize that you are trying to copy the same file to itself and skip it, but if you want to avoid that wasted work you can use 'for' and 'if' (or something) to only run find on the subdirectories of DIR/DIR/.
You can also use rsync, which has options that can delete files from the destination directory if they have also been deleted from the source directory, and many other such variations.

Resources