cmd, Open file in Command Prompt shell without knowing its extension - windows

I would like to run a file, in cmd without knowing the type (e.g. JPG, mp3, txt, mp4 and etc).
I tried to use 'start' command, but start (as far I found) need to know the full name of the file (with extension) and which program it should use to run it (e.g. VLC, notepad, photo viewer).
So, for example, instead of using C:\AI>start "VLC media player" "Bernard.avi" I want something like C:\AI>start "Bernard" to work similar.

If you are sure only one file has that filename you use a for loop to search for the file like this:
FOR %A IN ("C:\path\to\directory\filename.*") DO start %A
In your example it would be:
FOR %A IN ("C:\AI\Bernard.*") DO start %A
You can put it in a batch-script:
#echo off
FOR %%A IN ("%~1.*") DO start %%A
and give the path to the filename without extension as argument (don't forget the surrounding double quotes).
If multiple files have that filename it will open them all.

On linux terminal you could do something like this vlc Bernard.*,maybe you could try the * extension for windows.

Related

cmd file for reading in folder and save specific filename as string

I currently try to write a short cmd file, which finds a specific file extension in a folder and saves the whole file name as a string, so it can then start a python script. This is what I have:
for %i in (*.py) do set filename= %i
"C:\Users\blabla\python.exe" %filename
I was looking for hours for a solution and this was the closest I got. I can't imagine, that it is that difficult.
Actually I don't even need the for loop, since only one .py file is in the concerning folder, but I didn't manage to find a solution. The *.py file changes from time to time depending on which version is used. Just clicking the cmd file would be easier than typing "C:\Users\blabla\python.exe" %filename by my own everytime, that's why I want to use a cmd file.
It's just for playing around and learn how to use cmd prompt works
What I want written as an example:
§get whole filename of file with .py extension§
set filename = XY.py
§run
"C:\Users\blabla\python.exe" %filename

Listing the files inside a folder using batch file

I am using a batch file to view the contents (xml files) of a certain folder.
I need to use a batch file for it.
Found this command on internet and it is working perfectly when I type in the command prompt, but when I type it in and save as a batch file, it doesn't give any out put at all. (Basically not running the content)
FOR /R D:\Myfolder %F in (*.*) do rename %~nF.xml %~nf1.xml
There are no restrictions in the folder either.
Shouldn't have worked from the prompt.
In a batch file, you need to change any % for the metavariable (%F in this case) to %%.
Having said that however, (or, from the prompt, %F and %f) are two different animals. It's virtually the only situation where batch is case-sensitive. Your command attempts to rename files using their NAME only (~nF) so if it was to encounter fred.txt it would attempt to rename Fred.xml to -er, 1.xml (I think, maybe %~nf.xml)
Best to say what you're trying to do. We're reasonably slick at crufting up solutions...

Easy task, windows prompt batch file

I am a Windows 7 User who wants to convert multiple *.eps files to pdf-files. Therefore I installed a program called EPSPDF.rb.
I integrated it into my systempath which makes me use it in the command prompt like this:
C:\TEMP>epspdf somefile.eps
The program converts somefile.eps to somefile.pdf.
What I am trying to achieve now is writing a Windows Batch File which will look in
the directory C:\TEMP for all *.eps files and convert them all.
I am still trying hard since I am quite unfamiliar with the programming language.
I guess writing the few lines will be an easy issue for someone who is familiar with Batch Files in Windows. I will be very grateful for any help!
for %%f in (*.eps) do epspfd %%f
This is for use in a batch file. You can run it right on the command prompt, only you need to use %f instead of %%f. Just because. ;)
for %F in (*.eps) do epspdf %F

How to delete a folder that name ended with a dot (".")?

I got some folders created by malware whose name ended with a dot like C:\a.\ or C:\b.\, etc.
I found a solution that can remove such folder with command rd /q /s "C:\a.\" but if I call win API RemoveDirectory, it returns ERROR_FILE_NOT_FOUND.
And I just wonder how to write a function to delete such directory, thanks
I test on my own Windows XP SP3 system like this
create a folder C:\>mkdir a..\\\ and I cannot double click to access this folder. and I can remove with command rd /q /s "C:\a.\"
what Windows system API(s) that rd /q /s command call?
Here's a solution to this problem:
rd /s "\\?\C:\Documents and Settings\User\Desktop\Annoying Folder."
Solution:
When you call RemoveDirectory, make sure that you prefix the path with the string "\\?\".
Explanation:
It has everything to do with the dot. According to MSDN, there are certain cases where you may not be able to delete a file or folder on an NTFS volume, specifically when the file name is invalid in the Win32 name space (which is why you are unable to open the file using the normal methods in Windows Explorer).
You may not be able to delete a file if the file name includes an invalid name (for example, the file name has a trailing space or a trailing period or the file name is made up of a space only). To resolve this issue, use a tool that uses the appropriate internal syntax to delete the file. You can use the "\\?\" syntax with some tools to operate on these files, for example:
del "\\?\c:\path_to_file_that contains a trailing space.txt "
The cause of this issue is similar to Cause 4. However, if you use typical Win32 syntax to open a file that has trailing spaces or trailing periods in its name, the trailing spaces or periods are stripped before the actual file is opened. Therefore, if you have two files in the same folder named "AFile.txt" and "AFile.txt " (note the space after the file name), if you try to open the second file by using standard Win32 calls, you open the first file instead. Similarly, if you have a file whose name is just " " (a space character) and you try to open it by using standard Win32 calls, you open the file's parent folder instead. In this situation, if you try to change security settings on these files, you either may not be able to do this or you may unexpectedly change the settings on different files. If this behavior occurs, you may think that you have permission to a file that actually has a restrictive ACL.
(Source: http://support.microsoft.com/?kbid=320081)
Ive posted this on SU and I decided to post it here too. Its the simplest and fastest and easiest way to achieve this. I am now laughing at how much simple it is.
Install WinRAR
Follow the Step by Step procedure from pictures:
I myself had WinRaR installed so I decided to demonstrate the workaround in it.
This workaround is also possible by using 7zip.
One another thing I should mention is that, as it seems the problem is caused by using windows explorer and any other file browser (like winrar file browser itself, ftp explorers etc.) will treat this files as normal.
You could try using any file browser and simply delete those files and not bother archiving them though!
Cheers!
If you have git installed (you can get ir from here) then it is as simple as:
Navigate File Explorer to location where problematic folder is located.
Context menu (right mouse button) > Git Bash Here.
rm -rf Foldername./
When you see the name is "a.", but the actual name is "a.."
Try this:
rd /q /s "C:\a..\"
And you can try explore the folder by this code:
for /f "tokens=3 delims=<>" %%a in ('dir /ad /x "C:\*" ^| findstr " a\.\.$"') do (
for /f "tokens=1" %%b in ("%%a") do start "" "%%~fb"
)
I used "WinRar" A simple RAR, ZIP processor. You can use any sort of file name editor. Just open the directory where your file is into WinRar and select rename after right clicking the file/folder you want to rename and fill in the new name.
If you need to keep the data you can also use the \\?\ trick for renaming the folder.
ren "\\?\C:\Documents and Settings\User\Desktop\Annoying Folder." "\\?\C:\Documents and Settings\User\Desktop\Annoying Folder"
This is an ideal solution if you need to know what is inside the folder or if the data is important.
This works in both Command Prompt and PowerShell.
Try to use unlocker program to delete files and folders that you can't delete normally.
if you want to keep the files theres options in bash as well.
you will require the Windows Subsystem for Linux package (i have Ubuntu installed)
to keep the files. open a command prompt and cd over to where the file or folder is located.
now type "bash"
this will open bash in the prompt. now enter mv '[folder or file you want to move]' '[new name (can include path)]' (theres more to mv so if you want to read up on all of its options use 'man mv' this will open its manual page (then use q to return to bash))
the mv command is short for move, but its has a secondary function of renaming things.
also in bash use 'single quotes' and not a normal "double quote", as bash expects 'single quotes'.
heres a example. assume your folder is named "data 1." located in c:\users (so the full path to the error folder is c:\users\data 1.
1. open command prompt using any method
2. enter cd c:\users
3. now type bash this loads bash in the folder you previously were in
4. finally type mv 'data 1.' 'data 1'
5. the folder is now accessible and you can choose to delete it.
Use bash rm command from Ubuntu on Windows 10

Opening multiple PDF documents using batch file

I am trying to open several PDF documents using a simple batch file:
ECHO OFF
CLS
cd Program Files\Adobe\Reader 9.0\Reader
Acrord32.exe C:\Users\BW1.pdf
Acrord32.exe C:\Users\BW2.pdf
Acrord32.exe C:\Users\BW3.pdf
Acrord32.exe C:\Users\BW4.pdf
Acrord32.exe C:\Users\BW5.pdf
Acrord32.exe C:\Users\BW6.pdf
EXIT
The above batch file opens the first PDF only, then waits until I close it for the next PDF file to open. How can I have all the PDF documents open at the same time? (Like going to Acrobat Reader, file->Open->xx.pdf)
Use start:
start acrord32.exe 1.pdf
start acrord32.exe 2.pdf
start acrord32.exe 3.pdf
Or even (as Johannes Rössel suggests in the comment below):
start 1.pdf
start 2.pdf
start 3.pdf
Would probably work as well (depending on your default PDF viewer).
Note that when using start you have to be careful when using quoted arguments, as the following won't work (the first quoted argument is interpreted as the title for a new console window):
start "1.pdf"
Instead you'll have to do the following:
start "" "1.pdf"
It's an annoying quirk of start, but you have to effectively supply a dummy title in this case to properly open the specified file (even though the title is unnecessary as this won't create a new console window).
A list of other available batch commands.
For me it works even without the start command. I use:
c:\path\to\my.pdf
in cmd.exe windows frequently, and it always opens Acrobat Reader (my default viewer on Windows). In a batchfile I've written to generate PDF via Ghostscript, my last two lines are:
"%ouptutpath%\%outputfile%.pdf"
"%outputpath%\%outputfile%-optimized.pdf"
which automatically opens both generated PDFs in two different Reader windows. (My %outputpath% contains spaces, the %outputfile% may also have some...)
Have you tried whether Acrobat Reader allows for more files on the commandline, ie.
start acrord32.exe 1.pdf 2.pdf 3.pdf
Thank you!
Using start did the trick. I had to use start as many times as the number of pdf documents I want to open. For some reason
start acrord32.exe 1.pdf 2.pdf 3.pdf
opens only the first document. So I guess Acrobat reader might not allow for more files on the command line.
I rally appreciate your answers.
Thanks for the above answers.
I also tried below, working fine:
start /B excel.exe "D:\my first file.xlsx" "E:\my second file.xlsx" "D:\working folder\my third file.xlsx"
For every pdf file in the specified directory, use the start command on that file:
for %f in ("C:\Users\*.pdf") do start %f
As per the Microsoft Docs:
For runs a specified command for each file in a set of files.
for {%variable|%%variable} in (set) do command [ CommandLineOptions]
This is follow up to the answer given by JSON C11 above.
I checked in Windows 10 OS, the command given as below with the error ("C:\Users*.pdf") was unexpected at this time.
for %f ("C:\Users*.pdf") do start %f
What is missing is 'in'. Correct code is...
for %f in ("C:\Users\*.pdf") do start %f
If you have a binary to open that particular type of file, and you like to open in maximized view, you can use the following code.
for %f in ("C:\Users\*.pdf") do start /max <path to binary> %f

Resources