symbolic link to a text file in command line - windows

Im trying to create a symbolic link to a text file with directory:
C:\users\me\textfile.txt
where the shortcut is in the same folder (for simplicity)
using command:
mklink /d "C:\users\me\textfileshortcut.lnk" "C:\users\me\textfile.txt"
but this just creates a folder with the name textfileshortcut.lnk and not a shortcut to a file.
What am i doing wrong?
I dont have too much experience with command line but i need it for this one task.

Create the symlink for a file using the below cmd command.
mklink "C:\users\me\textfileshortcut.lnk" "C:\users\me\textfile.txt"
/d or /D will create the directory symlink.

Related

Windows context menu run hidden xcopy

I am trying to add a new option to the context menu for folders in Windows. I have managed to add the option and specify its command as follows:
xcopy.exe "%0\*" "c:\Destination\" /EHY
This code is added to regedit.exe
I have a folder in the c: drive named Destination. I am trying to copy the folder that I right clicked to the Destination folder, without a command prompt window.
What is happening: xcopy is running and copying the content of the folder and in the foreground. Please help me with these two issues:
Run the xcopy command without showing a window.
Copy the folder to a new folder in Destination named after the copied folder.
Thank you.
The command that satisfies the two issues listed is at the very end. First, some notes of explanation.
When you add a shell command to the Windows Registry, you have several variables available to you (such as %1, %L, and %V). Now, you would like a new folder in Destination named after the copied folder. Parameter extensions (such as %~n1) can strip everything from the full path and give you the name of the directory leaf.
However, these are not available when using the shell command from the Windows Registry. The most straightforward way to get a plain directory name is to create a temporary batch script, run it, and delete the batch script afterwards.
The following will copy the selected directory as a sub-directory inside Destination:
cmd.exe /c echo #echo off>"C:\Destination\_tempxcopy.bat" & echo xcopy "%%~1\*" "C:\Destination\%~n1" /ECIQHY ^>nul>>"C:\Destination\_tempxcopy.bat" & call "C:\Destination\_tempxcopy.bat" "%1" & del "C:\Destination\_tempxcopy.bat"
This next part requires the use of a third-party utility.
The previous command will open a command window and leave it open as long as copying is in progress. To hide that window, use the tiny utility RunHiddenConsole
The following will copy the selected directory and hide the command window while copying:
"C:\Destination\RunHiddenConsole.exe" cmd.exe /c echo #echo off>"C:\Destination\_tempxcopy.bat" & echo xcopy "%%~1\*" "C:\Destination\%~n1" /ECIQHY ^>nul>>"C:\Destination\_tempxcopy.bat" & "C:\Destination\RunHiddenConsole.exe" /w "C:\Destination\_tempxcopy.bat" "%1" & del "C:\Destination\_tempxcopy.bat"
This could certainly be made more flexible and efficient, but the above command at least demonstrates the technique for accomplishing the task.

Open a .BAT file in another directory from within a .BAT file

I have a .BAT file on my desktop that calls another .BAT file in the same location named MySQL:
start MySQL
pretty complicated right? How could I access this same file if it was in c:\ProgramFiles? I know I need to first go up one directory, but I cannot for the life of me make it work. I'm using windows 10 by the way.
You should do like this example :
#echo off
CD /D "%Programfiles%\Mozilla FireFox\"
Start "" Firefox.exe
and in your case it should be like this :
#echo off
CD /D "%Programfiles%\Sqlfolder\"
Start "" Mysql.exe

Batch file replacing issue

I'm trying to change my group policies by replacing the scripts.ini file in C:\Windows\System32\GroupPolicy\Machine\Scripts by using a batch file. The batch file is on my desktop in a folder called replacer, the custom scripts.ini is in the same folder. When i right click the batch file and "Run as administrator" it suddenly can't find the scripts.ini file that's in the same folder. When i don't run as administrator it finds it, but can't replace the scripts.ini file in group policies.
Edit:
Here's the code(1 line):
xcopy /s/y scripts.ini C:\Windows\System32\GroupPolicy\Machine\Scripts
When you run a batch script by double clicking it, the current directory will be the folder where the script resides.
But when you run the script as Administrator by right mouse clicking, then the current directory is something else, typically C:\wINDOWS\system32.
Your script can use %~dp0 to get the full path of where the script is installed, so you can simply prefix your source file with that path:
xcopy /s/y "%~dp0scripts.ini" C:\Windows\System32\GroupPolicy\Machine\Scripts
If you have additional commands that depend on the current directory, then I suggest you use PUSHD to change your current directory instead
pushd "%~dp0"
xcopy /s/y scripts.ini C:\Windows\System32\GroupPolicy\Machine\Scripts

Why cannot i use batch XCOPY while running in admin mode?

i have run very simple script:
xcopy some.exe c:\folder\ /h/y and it works normally. but when i try to run .bat file with this code as administrator - cmd line opens for a moment but nothing happens (file not copied). Can anyone explain this issue?
i also tried to use echo xcopy instead of xcopy, but nothing had changed.
i need only admin running of .bat file, cause i want to copy file in \windows\system32 folder
when you start a batchfile as administrator, it's working directory is C:\windows\system32\. So your script doesn't find your file. Either work with absolute paths, or change the working directory.
You can change it to the directory, where your batchfile resides with:
cd /d "%~dp0"
Note: to keep the window open to read any errormessages, append a pause command.

CMD script that goes to a folder

I'm trying to make a CMD script that will do CD Desktop\Crunchyroll\pyscripts and enter it, then I want to be able to type a command. Is there a way I can make a script to start in the Desktop\Crunchyroll\pyscripts directory on my computer?
Put this into a blank text file called MyScript.CMD
Double click it.
#echo off
CMD /K CD /D "%userprofile%\Desktop\Crunchyroll\pyscripts"
To create a symbolic link on Windows, open Command Prompt (as an Administrator). Let's say your username is Jason. And the symlink we will name 'code'.
cd \Users\Jason
mklink /D code Desktop\Crunchyroll\pyscripts
cd code
dir
All your files in pyscripts will be listed. You basically have created a shortcut to your pyscripts.
Personally, I'd simply create a shortcut to cmd.exe and set Properties/shortcut/Startin to Desktop\Crunchyroll\pyscripts.
If you really want to jump to pyscripts from within an existing cmd instance, then create a textfile named 'pyscripts.bat' containing
#echo off
CD "%userprofile%\Desktop\Crunchyroll\pyscripts"
and store it somewhere in your PATH.
Then executing pyscripts from within a cmd instance will jump to the indicated directory and
....
PUSHD
call pyscripts
{do some python}
POPD
...
should allow you to execute python from that directory within a batch file.

Resources