Move a specific file from one folder to another on Windows 10 in specific periods by using script - windows

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).

Related

Bat file to remove all user folders with 20 in the name bat file

I just started working in a school and the way we have the computers set up they automatically download a students user from the network when a student logs into any computer. We're working on making a script to run this command for cleaning them :
for /D %f in (*20**) do rmdir %f /s /q
In command prompt it shows every user with 20 in the name and deletes them from the computer along with everything in these folders.
We can't just change the file name to .bat instead of storing it in a note pad to copy to a command prompt window.
The batch file we currently have is
#echo off
pause
for /d %%f in (*20**) do rmdir C:\Users\%%f /s /q
pause
Which shows each pause to try to help me debug it but doesn't delete the folder. What am I doing wrong here?
I wouldn't do this. As I assume it's something like:
Regular%20User
%20 is basically a space.
To address your issue of wanting to clean them, as I assume it is the temp profile that is created when they login(from a network profile), rather than making the script clean anything with 20 in it, make a script that removes users locally that are of a certain Group Policy group. That way your admin accounts stay locally, and if your script is done right, your student's profiles are autocleaned after a set delay. (I would recommend 30 days, as you can use the temp profile as evidence if a student is caught on a computer he/she shouldn't be on :)

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

How to add batch script to context menu of multiple computers

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"

Batch file to start all programmes in the start folder of XP

I need start all folders in a the Windows "Start/Programs/Startup folder" of an XP machine, explorer is disabled to stop top people playing and remove the Start and Task-bar.
I can run a batch file at start-up but how do I write the batch to run ALL programs in the "Start/Programs/Startup folder" the programs in the folder may change but the batch needs to remain the same
I am able to open each file individually using the below code but I really need to be able to open everything in the folder to avoid problems in the future
start "" /b "C:\Documents and Settings\User\Start Menu\Programs\Startup\PROG.appref-ms"
I have tried the code below, that batch starts but nothing starts
%DIR%=C:\Documents and Settings\Pete\Start Menu\Programs\Startup
for %%a in (%DIR%\*) do "%%a"
Running the batch from the desktop also doesn't run the programs in the start folder, the DIR address is taken from windows explorer when I navigated to the folder with the short cuts in
This is an interesting request--one that I would question the motive behind, but since you asked, here's a way you could accomplish it:
#echo off
set DIR=C:\Your\Directory
for %%a in ("%DIR%\*") do "%%a"

Copy a .bat off a USB and copy it to the Startup folder script - WinXP

I'm a Mac/iPhone dev so I don't know very much about Windows scripting...
The point is I have to install a startup app on many computers, so I'd like to have a USB stick with two .bat files:
would be the actual "app"
would be the script that would copy the 1st.bat off my USB to the Windows startup folder...
How can I do that?
the name of my usb is "USB" and the name of my startup app is "startup.bat".
How I already said, I'm extremely lame in Windows programing, and I need it acutely ;)
Thanks A LOT!
Try the following script. This will cause the application to run whenever the current user logs in. Without administrative privilages, you won't be able to do it for all users in one go.
#Echo Off
CD /D %~dp0
Set StartupFolder=%AppData%\Microsoft\Windows\Start Menu\Programs\Startup
If Exist "%StartupFolder%" Goto :FoundStartup
Set StartupFolder=%UserProfile%\Start Menu\Programs\Startup
If Exist "%StartupFolder%" Goto :FoundStartup
Echo Cannot find Startup folder.
Exit /B
:FoundStartup
Copy "MyApp" "%StartupFolder%"
Each line does the following:
Turn off command echoing, making the script look cleaner to the end user.
Set the current directory to wherever this script is located.
Set the Startup folder's path as expected in Windows Vista or later.
If this folder exists, jump to the copying stage.
Set the Startup folder's path as expected in Windows 2000 or later.
If this folder exists, jump to the copying stage.
Report that the Startup folder can't be found.
Exit the batch script.
A label that can be jumped to.
Copy "MyApp" from the current folder (USB) to the Startup folder.
I don't want to take credit for Hand-E-Food's answer, but I figured out why his code didn't work and I can't reply to his answer, so here it is. Instead of using quotes around the %StartupFolder% variable in the Copy line, use them around the path for Set StartupFolder. Therefore, the code would be as follows.
#Echo Off
CD /D %~dp0
Set StartupFolder="%AppData%\Microsoft\Windows\Start Menu\Programs\Startup"
If Exist %StartupFolder% Goto :FoundStartup
Set StartupFolder="%UserProfile%\Start Menu\Programs\Startup"
If Exist %StartupFolder% Goto :FoundStartup
Echo Cannot find Startup folder.
Exit /B
:FoundStartup
Copy "MyApp" %StartupFolder%
The only reason I figured this out is because it wasn't doing anything for me either, so I tried removing the quotes around %StartupFolder% and that resulted in an error message that it couldn't find the folder, but at least I knew it was doing something at the end. Once I figured out that it was looking for the wrong folder because it thought the folder name stopped at the first space in its name, I simply added in the quotes and voila!
Try this (replace app.bat with whatever your actual app is called). This should work on Windows 2000 and up.
IF EXIST "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\*.*" COPY APP.BAT "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\"
IF EXIST "C:\Documents and Settings\All Users\Start Menu\Programs\Startup\*.*" COPY APP.BAT "C:\Documents and Settings\All Users\Start Menu\Programs\Startup\"

Resources