I have a bunch of folders named like "XXXXX John Doe". I know the X part, not the name afterwards, however, the X part is always unique. Manually i could just TAB, however i don't know how to do it via cmd only.
I used to be able to just do this:
"! \n explorer.exe " + rootdirectory + foldername
Now, since the folder structure naming scheme changed, im struggling finding a way to open folders, regardless of it having a (name) suffix or not.
This question is for Microsoft's cmd and it's gonna be called by a VisualFoxPro program.
You could ADIR() in VFP to get the actual name of the file and then use that in the call to Explorer. Presumably you know the name of the folder that contains the folder you're looking for, so something like this:
LOCAL aFolder[1], nFolders, nFolder
CD <the parent folder>
nFolders = ADIR(aFolder, '','D') && gives you all folders in the parent folder
IF nFolders = 1
nFolder = 1
ELSE
nFolder = ASCAN(aFolder, 'XXXXX', -1, -1, 1, 13)
ENDIF
* Now do your call to Explorer, passing aFolder[m.nFolder] as the folder name
Probably the shortest command is using ForFiles but do consider safety improvements below
forfiles /m XXXXX* /C "cmd /c explorer /e,#path"
OR slightly longer but more likely to only run with cd & folder thus not launch a similar filename* by mistake.
forfiles /m XXXXX* /C "cmd /c cd /d #path&explorer ."
This works on the basis that you are searching from the root folder and not somewhere a random explorer bat or otherwise gotcha is lurking.
first as #mofi suggests ensure explorer is well defined so replace that as %SystemRoot%\explorer.exe
/m is based on the premise it is a foldername rather than any filename that is found thus must be run in the sub folders root and you can enforce that by adding /p "rootdirectory" before /m
As constructed it works best if folders are exactly as specified i.e. a fixed unique ID followed by a space and then variable naming.
finally a shorter method but beware the need for escapes and also be aware that without the cd restriction it can launch any random files such as xxxxxx.txt thus I consider the "cd" method above as safest for calling a folder.
forfiles /p "rootfolder" /m XXXXX* /C "cmd /c start \"\" #path"
Related
In order to ask for the version of a file, the following commandline request can be launched:
wmic datafile where name="C:\\Windows\\System32\\msiexec.exe" get Version, Name /Value
The result looks like:
Name=C:\Windows\System32\msiexec.exe
Version=5.0.19041.1
I would like to launch this request for all specific DLL files in all subdirectories somewhere on my computer.
So I started as follows:
forfiles /S /M specific.dll /C "cmd /c echo #path"
I received the entire list of specific DLL files, mentioned as entire path names, which was exactly what I wanted. Now I would like to combine both approaches and you can guess, this is going wrong:
forfiles /S /M specific.dll /C "cmd /c wmic datafile where name="#path" get Version, Name"
I get following results:
Node - PORT-DDM (this is the name of my computer)
ERROR:
Description = Invalid query
You might think "Obviously, you are using double quotes within double quotes", but replacing the internal double quotes (") by double double quotes (like name=""#path"") does not solve the issue, and using single quotes neither. (Please don't tell me the issue is due to the single backslashes in #path)
Does anybody know how I can solve this, preferably without using a simple for-loop: I'm trying to understand how to replace the UNIX find ... -exec by Windows forfiles ... /C "cmd /c ..." ....
Edit: Maybe the question can be more general: apparently there is not one question on the entire site, containing the tags [forfiles] and [wmic]: is it possible that both commands don't go well together and why?
Given a UNC and a local Directory how would I go about copying files from remove to local in a simple, recursive fashion.
eg: - This works currently and copies all files within MyDIR to MyDEST:
SET MyDIR="\\123.10.10.10\Logs\Files\Server12\Folder1"
SET MyDEST="C:\Users\Desktop\Logs\Files"
robocopy %MyDIR% %MyDEST% /MIR
The issue I'm running into is that within MyDIR, Server12 has multiple (usually 3) folders stored and within them are the files I need so some level of iteration would help.
So I could try a bit of manual work and
SET MyDIR1=.....Folder1"
SET MyDIR2=.....Folder2"
SET MyDIR3=.....Folder3"
...as needed
but I'd also have to
robocopy %MyDIR1% %MyDEST% /MIR
robocopy %MyDIR2% %MyDEST% /MIR
robocopy %MyDIR3% %MyDEST% /MIR
which is easy enough but is there a way to loop through a list or even better, a way to recurse through all of Server12's folders and copy over all files?
I was thinking of using Powershell but I'm just trying to throw something together for quick, occasional use. I'm willing to but not sure if I should go in that direction as I have a bit of experience in it but not much.
I was able to get some for loops working but had errors when trying to get this to work with UNC the paths ( as well as using the %CD% variable to list the a UNC directory ).
In the end copy worked for my needs but a problem with my local paths containing a dash (yes a '-' because my company uses OneDrive it's in the path) was making it difficult to get this working. I passed FORFILES my variable, which contained the dash (demonstrated below).
In the end I had to use a temp folder....
SET MyRootDIR="\\123.10.10.10\Logs\Files\Server12"
SET MyDEST="C:\Users\Desktop\Logs - files\Files"
SET MyTempDEST="C:\LOGSTemp"
REM Delete these folders to empty them
DEL /Q %MyDEST%\*.*
DEL /Q %MyTempDEST%\*.*
PushD %MyRootDIR% &&(
FORFILES /M *.* /S /C "cmd /c copy #file %MyTempDEST%"
) & PopD
MOVE %MyTempDEST%\*.* %MyRootDIR%
I need the files only, because my next steps are to decompress the files and perform a keyword search within them outputting the results to a results.txt file.
Variables are at top to make it easier to change to difference server instances.
It works well but it has to be done in two steps. In PushD if I change it to to my variable %MyRootDIR% it gives me error :
ERROR: Invalid argument/option - '-'.
Tried escaping etc with no luck but in the end the above works
This command is supposed to copy multiple files from a static source folder into each folder for a set of saved web pages:
forfiles /m *.htm /c "cmd /c copy /y _core/*.* #fname_files"
However, each call fails with a status of, "The system cannot find the file specified."
If this is tried:
forfiles /m *.htm /c "cmd /c copy /y 0x22_core/*.*0x22 #fname_files"
the status displayed shows the name of each source file and the same error message.
I've also tried adding setlocal/endlocal around the call but it still fails.
Searching on the web brought lots of discussions but nothing showing forfiles, cmd, and copying into a destination directory using #fname.
Would someone with deeper knowledge of batch scripting "fix" this line so it works as intended?
If I understand correctly what you are trying to do :
1)You might be getting "The system cannot find the file specified." because the directory _core is not in the same directory as the *.htm files
2)In order to copy the *.htm files into each #fname_files folder, you must create the folder first. Here is the command line with mkdir added :
forfiles /m *.htm /c "cmd /c mkdir #fname_files & copy /y _core\*.* #fname_files"
I want to write a batch file to cleanup my Downloads folder by deleting everything in it Except Files and Folders that were created today.
Thanks.
If you are using modern Windows, recommend you use forfiles,
Folder can still messy. Do you want to do it based on the timestamp of the directory itself? Do you want to process recursively through all folders, deleting files based on date and then delete the folder if it empty after deleting the files of given age. There are other reasonable interpretations of your question as well. Personally, I use a python script so I can make the file cleanup do exactly what I want. This may also be why Uriil suggested PowerShell.
Arguably, Windows Services for Unix downloadable from Microsoft would be considered fair game (allowing the find command mentioned by Johnride). If you use this, make sure Johnride suggestion matches your actual intent. For the find command, using the option -print instead of -exec is great for debugging
If you can use forfiles, this article may give what you want. I taking the liberty of pasting in the batch file solution using forfiles from the article.
#echo off
:: set folder path
set dump_path=c:\shares\dump
:: set min age of files and folders to delete
set max_days=1
:: remove files from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c del /q #path"
:: remove sub directories from %dump_path%
forfiles -p %dump_path% -d -%max_days% -c "cmd /c IF #isdir == TRUE rd /S /Q #path"
I want to quickly search for a file given its name or part of its name, from the windows command line (not power shell). This is similar to opening explorer and using the search box at the top.
Note: dir can search based on a string template but it will not search in the subdirectories.
Note2: findstr can be used to search for a token inside files and has a recursivity flag; it's funny that a more complex find can be easily discovered ...
dir /s *foo* searches in current folder and sub folders.
It finds directories as well as files.
where /s means(documentation):
/s Lists every occurrence of the specified file name within the
specified directory and all subdirectories.
dir /b/s *.txt
searches for all txt file in the directory tree. Before using it just change the directory to root using
cd/
you can also export the list to a text file using
dir /b/s *.exe >> filelist.txt
and search within using
type filelist.txt | find /n "filename"
EDIT 1:
Although this dir command works since the old dos days but Win7 added something new called Where
where /r c:\Windows *.exe *.dll
will search for exe & dll in the drive c:\Windows as suggested by #SPottuit you can also copy the output to the clipboard with
where /r c:\Windows *.exe |clip
just wait for the prompt to return and don't copy anything until then.
EDIT 2:
If you are searching recursively and the output is big you can always use more to enable paging, it will show -- More -- at the bottom and will scroll to the next page once you press SPACE or moves line by line on pressing ENTER
where /r c:\Windows *.exe |more
For more help try
where/?
dir *.txt /s /p
will give more detailed information.
Problem with DIR is that it will return wrong answers.
If you are looking for DOC in a folder by using DIR *.DOC it will also give you the DOCX. Searching for *.HTM will also give the HTML and so on...
You can search in windows by DOS and explorer GUI.
DOS:
1) DIR
2) ICACLS (searches for files and folders to set ACL on them)
3) cacls ..................................................
2) example
icacls c:*ntoskrnl*.* /grant system:(f) /c /t ,then use PMON from sysinternals
to monitor what folders are denied accesss.
The result contains
access path contains your drive
process name is explorer.exe
those were filters youu must apply