What are ways to associate file from cmd?
For example,I want .txt file extension to associate with a typical program, through command prompt?
what is the correct file association cmd command?
One needs to use ftype and assoc commands as follows (and note that sequence matters):
ftype txtfile="C:\Program Files (x86)\PSPad editor\PSPad.exe" "%1"
assoc .log=txtfile
assoc .txt=txtfile
assoc .wtx=txtfile
or
ftype TIFImage.Document="C:\Program Files\MSPVIEW.exe" "%1"
assoc .tif=TIFImage.Document
assoc .tiff=TIFImage.Document
Note that I haven't MSPVIEW.exe installed so I can't approve your ftype assignment rightness. In my Windows ftype TIFImage.Document command output is as follows:
TIFImage.Document=%SystemRoot%\System32\rundll32.exe "%ProgramFiles%\Windows Photo Viewer\PhotoViewer.dll", ImageView_Fullscreen %1
File association with COMMAND PROMPT (using .bat file):
REG ADD "HKEY_CLASSES_ROOT\Applications\notepad++.exe\shell\open\command" /v "" /t REG_SZ /d "\"C:\Program Files\\Notepad++\\notepad++.exe\" \"%%1\"" /f
::or use ---------------> REG ADD "HKEY_CLASSES_ROOT\txtfile\shell\open\command" /v "" /t REG_SZ /d "\"C:\Program Files\\Notepad++\\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
ftype txtfile="C:\Program Files\Notepad++\notepad++.exe" "%%1"
assoc .txt=txtfile
NOTE! If typing directly in CMD(instead of .bat) then use single % (instead of double %%) and single \ (instead of double \)
The previous suggested solutions are unnecessarily complicated and risky. Just right click on the file, click "Open with" and indicate the App you prefer (flag for permanent replacement). Windows does all this automatically and without risk.
Related
I'm working on a batch file, and I need to add this entry:
doskey wfreerdp="C:\Program Files\wfreerdp\wfreerdp.exe" $*
to this regkey:
HKCU\Software\Microsoft\Command Processor
I played with the options of reg add, but I simply cannot get it to work.
I know it should be something like:
reg add "HKCU\Software\Microsoft\Command Processor" /v doskey wfreerdp = "C:\Program Files\wfreerdp\wfreerdp.exe" $*
but I don't which one and how to use the /t, /S, /d & /f flags.
If I manually add the entry:
doskey wfreerdp = "C:\Program Files\wfreerdp\wfreerdp.exe" $*
it works perfectly.
Alternativelly I tried creating the C:\bat\macros.doskey file, containing the doskey command:
doskey wfreerdp = "C:\Program Files\wfreerdp\wfreerdp.exe" $*
and the following in the batch file:
reg add "HKCU\Software\Microsoft\Command Processor" /v Autorun /d "doskey /macrofile=\"c:\bat\macros.doskey\"" /f
No success.
Any tips?
If you are trying to add the doskey command to your Command Prompt's AutoRun so that it will run every time you open cmd.exe, then you would need to run the following:
reg add "HKCU\Software\Microsoft\Command Processor" /v "AutoRun" /d "doskey wfreerdp = \"C:\Program Files\wfreerdp\wfreerdp.exe\" $*"
Let's break this down:
From the documentation at cmd /?:
If /D was NOT specified on the command line, then when CMD.EXE starts, it
looks for the following REG_SZ/REG_EXPAND_SZ registry variables, and if
either or both are present, they are executed first.
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
and/or
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
You already know you're placing the key into HKEY_CURRENT_USER\Software\Microsoft\Command Processor.
The /v switch specifies the value name to add/modify, which in this case is the AutoRun key.
The /d switch specifies what that key should contain, which in this case is the doskey command.
Since your command contains quotation marks ("), you would need to escape them so they are not processed by the command line (\").
my bat file:
#echo off
set targetfolder=%1
cd /d %1
del .
pause
my delete.reg file
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\Run Batch script]
#="Delete all"
[HKEY_CLASSES_ROOT\Directory\Background\shell\Run Batch script\command]
#=C:\delete.bat \"%V\"
The option is in the context menu but if I click Im getting this error:
This file does not have a program associated with it for performing this action. Please install a program or, if one is already installed, create an association in the Default Programs control panel.
[OK]
You don't really need a batch file, you should be able to run a command directly from the registry key.
Example:
#ECHO OFF
SETLOCAL
SET "SKEY=HKCU\Software\Classes\"
SET "EKEY=\shell\DeleteAll"
REG ADD %SKEY%Folder%EKEY% /VE /D "Delete &All Files" /F>NUL
REG ADD %SKEY%Folder%EKEY%\command /VE /T REG_EXPAND_SZ /D^
"%%COMSPEC%% /C PUSHD %%L && DEL *.*" /F>NUL
Right-click on a folder and select 'Delete All Files' to delete all normal files within that folder. (Be careful this will be catastrophic if you select the wrong folder)
I am trying to add registry entry from bat file. I have a reg file that contains
[HKEY_CURRENT_USER\Software\Policies\Microsoft\Internet Explorer\Control Panel]
"HomePage"=dword:00000001
It works if you double click the reg file. However, I need to make the same registry edit from bat file. I have tried this
reg add HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel /v HomePage /t REG_DWORD /d 0 /f
But doesnt work. What is the correct way to do that ? (except running .reg file from bat file)
Put quotes around the key name.
reg add "HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel" /v HomePage /t REG_DWORD /d 0 /f
Because "Internet Explorer" and "Control Panel" contain spaces, you need quotes so it is all treated as one parameter.
Hello I need a batch for the following process:
A ton of files from sub folders must use the converter to convert filetype from .m2 to .m2_lk
without copying the files in a new folder or something like this.
For example file "tree.m2" is in an sub folder of an sub folder must use the "converter.exe"
I have tried this code but it doesn't work:
FOR /D "X:\XYZ\M2\ToLkM2\" %%G IN (*.m2) DO converter.exe %%G
It looks to me like you simply used the wrong FOR option. You want /R, not /D. Also, The file spec should be quoted on your conversion command line, just in case there are spaces or special characters.
FOR /R "X:\XYZ\M2\ToLkM2\" %%G IN (*.m2) DO converter.exe "%%G"
for /f "tokens=*" %%i in ('dir X:\XYZ\M2\ToLkM2\ /b /s^|find ".m2" /i') do converter.exe %%i
You can try using FORFILES command:
FORFILES /s /M *.m2 /C "cmd /c converter.exe #file #file_lk "
Option /s enables recursing to subdirectories. Read here about many file related substitutions that can be used in command section.
I have written code in window batch file (eq. getFiles.bat ) that get all files within select date range.
eq.
xcopy /S /D:01-10-2011 *.* C:\todaysFiles
But I want get all files in between two dates including From date and To date.
file extension is .cmd or .bat
If you're on Vista/Win7/WinServer2008 you can use robocopy like so:
robocopy c:\source c:\destination *.* /MAXAGE:20101231 /MINAGE:20111001
On XP, I'm not sure if there are built-in solutions short of using Powershell and the like.
The title of this question is slightly misleading. Based on the questioner's xcopy example, I believe the questioner wants to know "How to copy all files created...", not how to get all files (which I equate with list).
The questioner also states "file extension is .cmd or .bat". This could mean that the questioner wants to copy only files with these extensions but I think it means the questioner would like a batch script solution.
To use the following solution, open a command prompt and chdir to the folder where the files to be copied are located. Then enter the commands listed below, changing the SET values as appropriate.
SET DESTINATION=C:\destination
SET DATE_FROM=01/01/2005
SET DATE_TO=01/01/2007
> nul forfiles /S /D +%DATE_FROM% /C "cmd /C if #isdir==FALSE 2> nul forfiles /M #file /D -%DATE_TO% && > con ( echo #path && copy /V #path %DESTINATION% )"
Note: this will copy files in subfolders as well as files in the top-level folder.
The SET values could be hard-coded directly into the > nul forfiles... line, meaning only one line is required, but for clarity I've used variable substitution.
A caveat is that it is based on date modified (original question asked for date created)
Credit to aschipfl (https://stackoverflow.com/a/36585535/1754517) for providing the inspiration for my answer.
You can also use the forfiles command. It can search recursively in all subfolders /s for files created in the selected root folder /p <Path>, execute commands on the selected files /c "<Command>", apply masks to search /m <SearchMask>, between a date range /d [{+|-}][{<Date>|<Days>}]
more info here, and here
Here's a batch for viewing files by creation date by year. Easily changed to cmd prompt by removing the extra percentage symbols.
for %%a in (2011 2012 2013 2014 2015 2016 2017) do (
for /f %%i in ('xxcopy i:\podcasts\*.* /LL /ZS /Q /FC /DA:%%a-01-01 /DB:%%a-12-31 ^| find /c /v ""') do echo %%a: %%i
)