Batch file to copy files - windows

Need to have a batch file that will actually check if the file name has "abc" in its name and if its there , then it will initiate a copy.
So suppose folder /test has 5 files
apt.text
mar.text
may.text
Jan.text
abc_xyx1234.text
So since "abc" appears in string only in the last file , it should pick only last one.
I tried
#echo off
for /f %%a in (' dir /b /a-d ^| find /v /i "abc" ') copy "%%a"
pause
but it didn't work out.

You have no destination for your copy.
The find /v /i will find those filesnames that do not (/v) match abc
It's probably easier to use
copy *abc* destination

I think part of your problem is the following:
Even though forward slashes work sometimes, Windows' canonical path separator is the backslash \. Using forward slashes as path separators confuses programs which use the forward slash as parameter prefix, and most if not all of the Microsoft Windows tools do so, including FIND and FINDSTR.
Also, if you're talking about /folder - which should be \folder as explained above - you're talking about a directory in the root of the current drive. I'm not sure you really mean that.
Apart from that, Magoo's suggestion is still kind of valid, but it omits the path "problem".
So let's use absolute directories to clarify:
I'll assume your source directory is c:\work\source and contains the file set you've stated in your question. I'll assume your target directory is c:\stackoverflow\target. So you can do the following from anywhere on your system:
copy c:\work\source\*abc* c:\stackoverflow\target
If you're doing this from an arbitrary folder on drive c, you could omit the drives:
copy \work\source\*abc* \stackoverflow\target
And, if you're inside the source folder, you could use Magoo's answer:
copy *abc* \stackoverflow\target

Related

CMD DIR filename and path

I perform a search in the CMD shell.
The goal is to find all files with an x from DRIVE C. I only need to see the path and the filename.
I have tried this so far. If I insert the following, I only get a path.
DIR "C:\*x*.*" /s /b
How can I also return the filename?
You could use the Where command too:
Where/R C:\ *x*
You can add an /F too which places all output files into double quotes.
Where/F /R C:\ *x*
Drives require terminating with a back slash as above directories do not.
Where/F /R C:\Users\belsober *x*
Note: I've maintained the search mask you used, just remember that this will also match every single file extension too, e.g. .xml, .xls, .txt, .ocx, .exe and .docx

VB script to list folders and contents

I would like to write a VB script that will:
List all folders named "abc", and also there contents, in share drive \share1
Currently these folders named "abc" are located all over the place in \share1, but they all have the same name.
The issue I have is that some of these paths are long paths, hence getting this working through PowerShell has been very difficult, as it keeps hitting the 260 limit.
Prefixing a path with \\?\ allows paths up to 32,000 odd characters. This allows apps to opt in to very long file names as they crash most programs. Now it depends on how you access the file functions if the component will allow you. CMD does support this.
so a command line
for /f %A in ('dir c:\windows\abc /s /b /ad') do dir "\\?\%A"
\\?\ works with unc paths too.
for /f %A in ('dir \\?\c:\windows\abc /s /b /ad') do dir "%A"
I think that RoboCopy (the Windows utility) is your best bet. This can handle long path names, and it can actually be used to just list files, rather than copy them, by using the
/L option (: List only - don’t copy, timestamp or delete any files.)
http://ss64.com/nt/robocopy.html provides a good summary of all the options.
You can also download a GUI for it which might make it a bit easier to work with.
Another option is to shorten the path via subst:
subst X: C:\very\...\long\...\path

Batch File to Delete Files in Folders

I have many folders in a directory that contain various files. Each filename begins with XXX_ where XXX could be the name of the folder the file is in. What I am needing to do is to go through all those folders and delete any file where XXX is the name of the folder that file is in.
Please have an eye out this question: Iterating through folders and files in batch file?.
I think this should help you.
Please let me know if you need further assistance.
EDIT #1
The joker character in DOS command line is *. Then, while searching a directory for certain files, you may consider your regular expression, that is, your XXX_, and complete it with *, this shall return only the files for which you're looking for.
This means that instead of *.zip pattern in one of the FOR loops given the linked question, your first FOR loop should contain your directory name, then take this variable concatenated with the * character to obtain only the files you're looking for.
For example, consider trying the following:
dir /s XXX_*.*
This should return only the files you're interested in, given the right folder name.
EDIT #2
Thanks for having precised your concern.
Here is a code sample that, I do hope so, should help. Now I know you say you have the looping correct, so that perhaps only piece of this code might be needed.
#echo off
setlocal enableextensions enabledelayedexpansion
for /F "delims==" %%d in ('dir /ogne /ad /b /s .') do (
for /F "delims==" %%f in ('dir /b "%%d\%%~nd_*.*"') do (
echo %%d\%%f
)
)
endlocal
This works and lists the files contained in subfolders from the current (.) folder.
I have tested it from the following folder:
C:\Docume~1\marw1\MyDocu~1\MyMusi~1
Where a 'XXX' folder is contained. This 'XXX' folder contains the following files:
Copy of XXX_blah.bmp;
XXX_blah.bmp;
XXX_1234.ppt;
XXX_textfile.txt.
From this structure, the output is:
C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_blah.bmp
C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_1234.ppt
C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_textfile.txt
I then suspect that putting a del instruction instead of an echo command shall do the trick. This means that to isolate the foldername itself from its path, you need to use the ~n instruction with your folder variable name like %%~nd, where your iterating folder variable name is %%d.
Furthermore, you could even use a parameterized batch file in the process, instead of hardcoding it, that is, if your 'set YourFolder =...' is part of your production code. This could look like:
#echo off
setlocal...
set root = %1
set root = %root:~1%
set root = %root:~0,-1%
...
endlocal
Instead of having '.' as pictured in my first FOR loop, your would replace it with "%root%" in order to consider your command line parameter instead of a hardcoded filepath.
I do help this helps, sincerely!
As Ron says, since the question is tagged "windows".
EDIT:
Ron's answer, which seems to have disappeared!, was to use del /s
EDIT2:
OK, it's valid only for file names, not for directories. For the directories you'd have to use something like sketched below.
Additional info: when you want to do the same thing recursively to files in a directory tree, and (unlike del) there's no command that already does the traversing for you, you can use the /R option of the for command.
To see the for command's docs, do e.g. start "for-help" cmd /k for /?.
Cheers & hth.,
– Alf
cd C:\"foldername"
del /s XXX_"*"
cls
exit

Replacing a file into multiple folders/subdirectories

Is there a way in command prompt to take one file and copy it into another folder and its subdirectories based on its name?
I have an image named 5.jpg that has been put in a sub-folder that is in every folder in a directory.
I want to do a search inside of the folder (with the old image) and its sub-folders and replace all of the results with the new image.
Probably there is one more (simpler) way.
Use the replace command:
replace C:\SourceFile.Txt C:\Some_Root_Folder_Which_Contains_Multiple_SubFolders /s
As the command itself says it just replaces the file, which already existed in sub-directories.
I'm not sure if I understood you completely. The following code will search for all occurences of 5.jpg in subfolders of C:\MyPath\ and replaces them with C:\NewImage\5.jpg. I did test it, so it should work.
FOR with parameter /R will help you here:
FOR /R C:\MyPath\ %%I IN (5.jpg) DO COPY /Y C:\NewImage\5.jpg %%~fI
If you want more information about what FOR /R does and what %%~fI means, have a look at FOR /? | more which gives nice explanations about the new Windows cmd possibilities that are used here.
To do this work with several files, both paths are needed enclosed in quotes:
replace "C:\*.Txt" "C:\Some_Root_Folder_Which_Contains_Multiple_SubFolders" /s
The asterisk to make changes on all files with the ".txt" prefix.

Windows .BAT to move all directories matching mask from dir A to dir B

I want to write a .BAT file to move all sub-directories (whose name matches a mask) of C:\WINNT\Temp to H:\SOMEOTHERPLACE.
So if my mask is ABC* then the directories :
C:\WINNT\Temp\ABC1
C:\WINNT\Temp\ABC2
C:\WINNT\Temp\ABC3
should be moved to
H:\SOMEOTHERPLACE
and everything else (including files, as opposed to directories, which match the mask) should not. I do want to move them and not copy.
Can anyone point me in the right direction ?
OK I've figured this out. If you write a movedirs.bat file containing the single line
for /d %%X in (%1) do move %%X %2\%%~nX
And then run it (with argument 1 being the mask for the directories I want to move and argument 2 being the directory I wish to move the directories to) as
C:\>movedirs.bat C:\WINNT\Temp\ABC* H:\SOMEOTHERPLACE\
It produces the effect I want.
The /d argument on the 'for' ensures that only directories are processed. The '~n' modifier on the %%X variable means that the original sub-directory name (as opposed to the entire path) is used as the target within the second command line argument.
Just for the sake of posterity in investigating this I did something similar with xcopy but then i would have had to get involved in deleting the source so for my purposes move works better but for the record here's the same idea wrapped around xcopy.
for /d %%X in (%1) do xcopy %%X %2\%%~nX /E /I
To process directories with as well as without extensions, for example "C:\MyDir*.MyExt" above command will need a combined (filename+extension) modifier "~nx":
for /d %%W in (%1) do xcopy %%W %2\%%~nxW /E /F /R /Y /I
[Comments are useless for structured answers so I'll repeat the comment here - with a couple of edits !]
Thanks for your answer but I've found that you can't use xcopy with a wildcard on the source. Or rather you can use wildcards but then you get only the directories being created without any content. So if you do this ...
H:\SOMEOTHERPLACE>xcopy C:\WINNT\Temp\ABC1 /E
... you'll get the the ABC1 directory copied to your current directory as you might reasonably expect but if you do this ...
H:\SOMEOTHERPLACE>xcopy C:\WINNT\Temp\ABC* /E
... you'll get each directory name present in C:\WINNT\Temp appearing in your current directory but those directories will be empty ! Please tell me I'm wrong but that's what I find !

Resources