I've been trying to setup a batch script that can be run from the context menu inside a folder. The purpose of the batch script is to populate the folder with a predetermined folder structure.
This will need to be replicated on multiple computers(Windows 7-10), so my idea was to have a network share with two batch files and one .reg file. One of the batch files labeled "install.cmd" would copy the other batch file labeled "Subfolders.cmd" to a folder on the C drive, and run the .reg file to install a shortcut on the context menu to the "Subfolders.cmd" batch file.
I have created the "Subfolders.cmd" batch file and it works, but it has to be run from inside the folder I want the folder structure setup in. I would appreciate any help on how to create the "install.cmd" batch script that would create a folder on the C drive, copy the "Subfolders.cmd" script into it, and run the .reg file to create the item in the context menu that would allow the "Subfolders.cmd" batch script to be run in selected folder.
I'd appreciate any examples and/or suggestions of more efficient ways of doing this. Thanks!
Update: As requested, I have posted the code that generates the subfolders inside an opened folder. It's pretty simple.
md "Folder1" "Folder1/Sub1A" "Folder1/Sub1B" "Folder2" "Folder2/Sub2A" "Folder2/Sub2B"
There is no need for all this copying.
"\\computername\sharename\folder\file.bat"
Will run a batch file stored on another computer.
Ditto the reg command
regedit /s "\\computername\sharename\folder\file.reg"
You can do the above with the older mapped drives as well as UNC.
You need to show your second script so we can see why it NEEDS to be in the folder.
EDIT
You need to specify full paths.
To get the starting folder use %V in the registry command. So (and lets get rid of the reg file) (add your bat instead of echo)
reg add "HKCR\Directory\Background\Shell\Test Command\command" /ve /t REG_EXPAND_SZ /d "cmd /k echo ""%V"""
In your batch use %1 to get the starting folder, and tilde (%~1) to remove quotes (see call /? for documentation). (remember create folders from the lowest level as higer levels get made automatically).
md "%~1\folder1\folder2"
Related
I know I can use the below code to move all files from a source folder to a destination folder but how would I do this within a specific period of time. For example, I want to copy files from a folder into another folder every day at 2:00am. How can I do this with a script?
echo off
set X=<days>
set "source=C:\<Source Folder Path>"
set "destination=D:\<Destination Folder Path>"
robocopy "%source%" "%destination%" /mov /minage:%X%
exit /b
In case you want to do something periodically in Windows, using a script, I would give you following advise:
Write a script which does what you need to do.
Configure task scheduler to run the frequency and time table of your choice: adding this to the script itself is not a good idea (imagine you restart your computer and you have forgotten to re-launch your script, then it won't be executed again).
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.
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
I am making a batch file that needs to copy another batch file into the Start Menu Start Up folder (the one used when a program launches on login/start up). Since the path uses the user's computer name eg. C:\Documents and Settings\User Name I need the batch file to get the user's correct name instead of the "User Name" or * (wildcard). Wildcards doesn't work as the batch file comes up with "the filename directory name or volume label syntax is incorrect".
I hope this is clear enough.
You can also try this:
cd %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup
It works in Windows 10. The %appdata% variable gives you your required username by default
Open a new command prompt window by executing cmd.exe or using the shortcut in Accessories in Windows start menu. Enter set and look on the list of environment variables predefined by Windows. You are mostly interested in USERPROFILE.
The following command can be used to copy a batch file with name AaRM.bat from a folder available for all users like the all users desktop folder to startup folder of the currently logged in user.
copy "%ALLUSERSPROFILE%\Desktop\AaRM.bat" "%USERPROFILE%\Start Menu\Programs\Startup"
The double quotes are important as the name of the batch file with path and the path to the startup folder both contain spaces.
Copying the batch file from your desktop folder to the startup folder of the other user is most likely not possible as the other user might have no permission to access anything in your user profile directory and below.
You can copy the batch file to distribute also to a different folder accessible for all users like "%ProgramFiles%" or %SystemRoot% as the batch file in all users desktop folder is visible for all user accounts on desktop.
Best would be to put the batch file into Windows directory (%SystemRoot% or %windir%) and create / copy a shortcut file (*.lnk) in / to startup folder of the other user accounts. The Windows start menu folders should contain only *.lnk files and not batch files and applications.
And last it would be also possible to create a shortcut in "%ALLUSERSPROFILE%\Start Menu\Programs\Startup" to the batch file in %windir% to execute this batch file for any user who logs in on this computer. Your batch file could contain at top something like if "%USERNAME%"=="your account name" goto :EOF 1 or more times with various user account names to prevent doing anything for 1 or more specific users.
try the following command if u r using win 7. never tried on win 8 though.
cd C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
works perfectly for me.
Stumped about nobody answering yet..
Why are we so lost?
echo %userprofile%
To know the name of current user
For copying
Copy /y %~f0 "%USERPROFILE%\%AppData%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Alternative
copy "path of file you want to copy" "path of the directory where you want it copied"
copy path of batch file path of startup folder
Copy /y "File-Address-to-copy-here" "where-to-copy-to-here"
(as above)
then save file as .bat
and you have a batch file, run as admin if in system directories.
copy "C:\Documents and Settings\%username%\Desktop\batch.bat" "C:\Documents and Settings\%username%\Start Menu\Programs\Startup\"
This should work.
This works:
#echo off
copy "C:\Users\%username%\Desktop\somefolder\example.bat" "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
Try this:
copy "CopyPath" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup"
Also, if you want to skip the username for the copy path, then use %username% instead of the actual username.
#echo off
copy "%~n0%~x0" "%USERPROFILE%\Start Menu\Programs\Startup"
*Use this code!
i know the answer,
type this in your batch file:
copy "copy file path" "paste file path"
if the file path has a username than type %ALLUSERSPROFILE% instead of the username (it will fill up automatically the username for every windows pc)
example: copy "C:\Users%username%\Desktop\New folder\hi.txt" "C:\Users%username%\Desktop"
that did this:
Example to what it did when opened
final answer:
copy "Path of file" "C:\Users%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
that would work
This should work:
copy "anything.bat" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup"
this copies .txt, .exe, .bat, more into startup just change the .bat to anything else
I made this in Windows 10
I have this .vbs script that I am trying to run on windows 7.
It has to run with full permissions and it has to do it automatically.
To be clear When the user double clicks on the file it will get the prompt that asks "to allow to run the file as an administrator", and then run with full permissions.
To do this I created a batch file (run.bat) that calls the script file
cscript "V02.vbs"
pause
then I created a shortcut for the batch file which I can choose to run as admin.
The problem I encounter now is that when I run the batch file as admin the folder changes to c\windows\system32. The script, batch file and shortcut are all in the same folder. is there a way to get the folder location?
I may have misunderstood...
The easiest solution would be to "hardcode" that path into the patch file with a -
cd \path\to\my\script
cscript myscript.vbs
pause
Use "modifiers with batch parameters". From the linked documentation:
%~dp1 Expands %1 to a drive letter and path.
Since %0 is your script, %~dp0 is what you want:
cscript "%~dp0V02.vbs"
pause
Note that %~dp0 includes the trailing backslash, so your v02.vbs file name above becomes quite hard to read.