Window Cmd Prompt - Move file with special character - window

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

Related

Is there a CMD command to move a file to the next higher folder level?

I have something like saved a file here:
C:\Test\test.txt
I now want to move them via CMD so that the path is as follows:
C:\test.txt
Is that possible?
The command is:
move c:\test\test.txt c:\
The first argument is the source file.
The second argument is the target file or target-directory.
IF you just want to move the file exactly one level up the tree, and you don't know the name of the target directory, then you can use the .. indicator which means the parent-directory .
example:
move c:\Test\test.txt ..
will move the file into c:\Test .
If any part of the path or filename contains spaces then double-quote the source and/or target name appropriately.
See the help for the move command help move.
Yes, it is possible to move one or more files or folders up one level in folder hierarchy by using .. as described by Microsoft in the documentation about Naming Files, Paths, and Namespaces.
The simple approach in a command prompt window would be:
for %I in ("C:\test\test.txt") do for %J in ("%~dpI..\") do move "%~I" "%~fJ"
The command line in a batch file would be:
for %%I in ("%~1") do for %%J in ("%%~dpI..\") do move "%%~I" "%%~fJ"
%~1 references the first argument passed to the batch file which can be a file/directory name without or with a relative or with an absolute path, or even a wildcard pattern to move multiple files/directories up in the directory tree. The files/folders can be passed to the batch file also with a UNC path.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
for /?
move /?

Windows 10 Batch File - IF Usage

I have a simple batch file that contains a single line:
if not exist "C:\users\fred\desktop\b\" ( md "C:\users\fred\desktop\b" ) &&copy /Y "C:\users\fred\desktop\b.txt" "C:\users\fred\desktop\b\"
You can see the intent - if a particular folder doesn't exist on the desktop, create it. Regardless of whether the folder already existed, copy a particular file into that folder.
Note that there are two commands on the same line - the IF conditional and the copy command, using the "&&" operator.
But when I execute it, it only ever works if the folder doesn't exist. Then it creates the folder and copies the file to it. If the folder already exists, then it does nothing. It's like it thinks the whole thing is in the IF condition, regardless of the '(' and ')' around the folder creation.
I would swear that this worked on earlier versions of Windows...but I could be wrong.
FWIW, Windows 10 32 bit.
The code you post did not work in previous Windows versions either.
As written, your code says if this folder does not exist, make the directory and copy the file. If the directory exists, it never gets to the copy the file part.
What you want is for the copy to work regardless of whether the directory had to be created or not, so you need to use two separate lines.
if not exist "C:\users\fred\desktop\b\" md "C:\users\fred\desktop\b"
copy /Y "C:\users\fred\desktop\b.txt" "C:\users\fred\desktop\b\"
As suggested in the comments by CatCat, how about using XCopy instead?
With a trailing backslash on the destination, it will be created if it doesn't already exist.
Example:
Xcopy "%UserProfile%\Desktop\b.txt" "%UserProfile%\Desktop\b\" /H /K /Q /R /Y
You can adjust the options as necessary, enter XCopy /? for the usage information.
The then-part of an if command extends to the end of the line and may include several commands separated by &, && or ||. This has been so since the days of Windows NT, when cmd.exe was first introduced.
To restrict the then-part and keep the script on one single line you can place the if in a parenthesized compound command:
( if not exist "C:\users\fred\desktop\b\" md "C:\users\fred\desktop\b" ) & copy /y "C:\users\fred\desktop\b.txt" "C:\users\fred\desktop\b\"
( if not exist "directory" md "directory" )
By placing the if in a parenthesized compound command we ensure that the then-part does not extend to the end of the line
&
Command separator.
copy /y "file" "directory"
Executed regardless of the exit status of the previous command.

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 shell - list of file as a command input

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.

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