How to open file with default application in cmd? - windows

I'm trying to open a file in it's default editor after the user has created the file. So far my script is:
#echo off
#echo --- Create A New File ---
#echo -
#echo Where should we put the new file?
set /p fileLocation=# %UserProfile%\
#echo -
#echo What do you want to call your new file?
set /p fileName=#
#echo -
#echo Almost Done! What is the files extension?
set /p extension=# .
#echo -
copy NUL "%UserProfile%\%fileLocation%\%fileName%.%extension%"
(ignore the extra echos and '#' those are just for fun)
After I click the file, it does the command: Choose Location > Choose File Name > Choose File extension. I'm almost done on what I want but theres one last thing. How can I get the file name that I created and then open in its default text-editor?

You can use start to open the file with the associated application.
Resources :
Open a File in the Default Application using the Windows Command Line (without JDIC) (waybackmachine capture from Oct 30, 2010)

In windows you can use start (http://ss64.com/nt/start.html).
start "" "%UserProfile%\%fileLocation%\%fileName%.%extension%"

You can also use explorer.exe/explorer to open the file (e.g. explorer file.txt). This also works nicely if you use WSL, especially with an alias like alias open="explorer.exe" so you can just call it like, e.g., open file.txt.

I achieved the correct way of FILE ASSOCIATION using these cmd commands.
this is just an example:
REG ADD "HKEY_CLASSES_ROOT\Applications\notepad++.exe\shell\open\command" /v # /t REG_SZ /d "\"C:\\Program Files\\Noteepad++\\notepad++.exe\" \"%1\"" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt" /v "Application" /t REG_SZ /d "notepad++.exe" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList" /v "g" /t REG_SZ /d "notepad++.exe" /f
assoc .txt=MyCustomType
ftype MyCustomType="C:\Program Files\Noteepad++\notepad++.exe" "%1"
(it's better to put them in .bat file)

Related

pwd alias on Windows cmd

I want to create a fixed pwd alias like in this topic, but I want to keep all aliases in one file as in this answer by Argyll. Currently my file cmdAliases.cmd looks like this:
#echo off
doskey ls=dir
doskey pwd=echo ^%cd^%
Running pwd command now prints:
ECHO is on.
I believe the spaces in the command are the problem. Is there a way to fix it using only the cmdAliases.cmd file?
The links you provided show how to do it.
I have my doskey macros in a file aliases.txt in my folder %USERPROFILE%
11:24:16 C:\Users\LotPings________________________________________
> type Aliases.txt
~=CD /D "C:\Users\LotPings"
\=CD \
-=CD ..
Alias=Doskey $*
Aliases=Doskey /MACROS:ALL
And an autorun entry which loads this file
> reg query "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v autorun
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
autorun REG_SZ Doskey /MacroFile="C:\Users\LotPings\Aliases.txt"
To generate this Autorun automatically copy the following lines into cmd line or a batch file
Set "Key=HKEY_CURRENT_USER\Software\Microsoft\Command Processor"
Set "Val=Autorun"
Set "Typ=REG_SZ"
Set "Dat=Doskey /Macrofile=\"%USERPROFILE%\Aliases.txt\""
reg add "%Key%" /v %Val% /t %Typ% /d "%Dat%" /f

Copy a file with batch in a user selected directory

i'm looking for a .bat file that, on opening, ask the user what folder he want to select, then, the batch copy a file (python script) in this folder and execute it
for now i use :
xcopy c:/pythonfiletocopy d:/destinationpath
but i dont find a way to make the user choose the destination folder
any idea ?
thank you
You can request user input using the Set command together with its /P option.
Here's an example which should accept typed or pasted input and should also accept drag and drop for directories not containing spaces:
#Echo Off
Set "_in=" & Set /P "_in=Please provide a directory for the file: "
If Defined _in If Exist "%_in%\" Echo XCopy "C:\pythonfiletocopy" "%_in%"
Pause
I have added an Echo on line 3, if you're happy with the output you can remove that.
I know you said you didn't need one but you could JScript launch a GUI directory browser for input.
Here's an example which may do that:
0</* :
#Echo Off
For /F "Delims=" %%A In ('CScript //E:JScript //NoLogo "%~f0" 2^>Nul'
) Do Echo XCopy "C:\pythonfiletocopy" "%%A"
Pause
Exit /B */0;
var Folder=new ActiveXObject('Shell.Application').BrowseForFolder(0,'',1,'::{20D04FE0-3AEA-1069-A2D8-08002B30309D}');
try{new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(Folder.Self.Path)};catch(e){};close();
I have added an Echo on line 4, if you're happy with the output you can remove it and line 5.

REG add Doesn't Run

I've written a batch file that tests for a user's Microsoft Office version, copy an Excel Add-In to their device from a shared drive, and add a registry key to their device.
Each individual action in the batch works as designed. But when I combine them all together, the file doesn't like the REG add command and completely closes the command window (even if I put Pause after the REG add line).
To troubleshoot, I created a new batch file and added pieces of my code to it, one section at a time, and tested the file each time a new section was added. Every section ran fine until I got to this section:
CHDIR "C:\Windows\System32"
IF NOT %ALREADY_ENABLED%=="TRUE" (
REM Add the new key value if it doesn't exist already.
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%PATH%" /f
)
To see if the problem was my IF statement, I commented out the REG add line and put ECHO Hello World inside the IF statement. The file ran just fine and gave the Hello World output as expected.
I know the REG add command works because I have a batch file that only includes that piece of code and it works just fine:
#Echo off
setlocal enableDelayedExpansion ENABLEEXTENSIONS
chdir "C:\Windows\System32"
SET PATH=\"C:\Program Files (x86)\Microsoft Office\Office15\Library\Cerner_AddIn.xlam\"
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%PATH%" /f
I think the problem has something to do with the PATH variable, but I'm dumbfounded as to why this works in one file but not the other. Is it possible that the value of PATH is somehow changing during runtime after it's been set?
I'm not sure how to trap the error to even see what error is being thrown here. Everything I've tried to handle the error with doesn't work and the command window closes. Any ideas on what I'm doing wrong here?
Maybe this code snippet could help:
SET "myPATH=C:\Program Files (x86)\Microsoft Office\Office15\Library\Cerner_AddIn.xlam"
IF NOT "%ALREADY_ENABLED%"=="TRUE" (
REM Add the new key value if it doesn't exist already.
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%myPATH%" /f
)
Note:
do not change system environment variable PATH; use another variable name (myPATH);
quoting in set "variablename=variable value";
quoting in if statement;
to trap any error in a batch file, use echo ON while debugging.
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax

Adding a new registry file through batch commands

So I've been trying to add a new registry key and it's been working fine, noting the fact that this addition is not going to harm my computer it's just for me to understand a few concepts. The problem that's been happening is as follows, when I run my batch file, it's displaying the following message in cmd:
off REG ADD HKLM\Software\Hello - 24919 /v Test /t REG_BINARY /d fe340ead
the "off" in bold is a little weird, I'm not sure what happened for it to appear and not add the key, like I said I'm using batch commands and basically writing to another batch file through my initial batch file and this is the code I used:
#echo off
set new=new2.bat
echo #echo off REG ADD HKLM\Software\Hello - %random% /v Test /t REG_BINARY /d fe340ead >>%new%
Help is appreciated please! and thank you!
PS: I've referred to the following question (adding a random key to the registry through a batch file) , and did exactly the same, it worked once but then I don't know what happened for the above to show and not add the key.
#echo off
set new=new2.bat
(
echo #echo off
echo ^>nul REG ADD "HKLM\Software\Hello - %random%" /v Test /t REG_BINARY /d fe340ead
) > %new%
You need to place the #echo off and the REG ADD in separate lines

Command Prompt Navigation: Navigating To An Unknownly Named Folder

So, is there any way to navigate your command prompt to/through a location which's name you don't know exactly?
For example the situation is i want to rename the sei.dat file and its located at:
C:/programdata/CsD2/Tools/("THE UNKNOWN VERSION NAMED FOLDER")/data/per/sei.dat
So as you can see i know where the software is installed however the folder after "Tools" folder is named after the version of the tools (example V0.1.2.6) and it changes often.
Is there any command or way to navigate the Command Prompt to the files location? Or if not is there any way to modify the file without navigating the Command Prompt to the location?
I just want to rename the file but i don't know the name of the folder marked as "THE UNKNOWN VERSION NAMED FOLDER"
Test this:
#echo off
for /f "delims=" %%a in ('dir /b /s /a-d "C:\programdata\CsD2\Tools\sei.dat" ') do set "folder=%%~dpa"
if not defined folder echo sei.dat was not found & pause & goto :EOF
cd /d "%folder%"
cmd /k

Resources