Batch - Move File to Network Drive without being logged in - windows

I have the following script which is setup as a task to automatically move a file to a mapped network drive. The problem is that this only works when a user is logged in and has an active Windows session open. However, if the user is logged off these backups wont happen as I believe it cannot find the network drive. This is running on a Windows 2003 Server. Is there anyway to alter the script to make sure it can connect to the networked drive while no active sessions are open?
The process I am using is to move the file, then delete the file to clear up hard-drive space, then a .exe is run to empty the recycling bin.
#echo off
move C:\StarshipBackup\*.* Z:\StarshipDataBackup
del C:\StarshipBackup\*.* /F /Q
C:\emptyrecycle.exe

You can mount the drive in the batch file. Add this before your move command:
net use z: \\yourserver\sharename
Of course, you need to make sure that the account the batch file runs under has permission to access the share. If you are doing this by using a Scheduled Task, you can choose the account by selecting the task, then:
right click Properties
click on General tab change account under
"When running the task, use the following user account:"
That's on Windows 7, it might be slightly different on different versions of Windows.

Related

Creating a .BAT file and Do Not Know the Name of Some Folders and Drive Names

I am creating a Windows 7 batch file. Its purpose is simple: to install Minecraft to the computer and auto save it every 5 minutes. The way of doing it is quite hard. Here's how it should work:
User inserts USB drive containing: Minecraft data, a bat file, and some sort of file to autorun the bat file.
The autorun thing opens the bat file.
The bat file copies a folder and its' files from the USB drive, which is at USBDirectoryName:.minecraft to the directory C:\Users\SomeUsername\AppData\Roaming
The bat files copy a file called Minecraft.jar to the Downloads folder.
The bat files run the Minecraft.jar file.
The user uses the Minecraft.jar file to open the actual Minecraft game.
The bat file detects that the actual game has opened and starts a timer for 5 minutes.
After 5 minutes the program copies the C:\Users\SomeUsername\AppData\Roaming.minecraft\saves\New World to USBDirectoryName:.minecraft\saves
The user finishes playing the game and the disconnects the USB drive.
When the user wants to play again, the user connects the USB drive and the process starts over again.
Here are the problems with this process:
I don't know the directory of the USB drive.
I don't know the username of C:\Users\SomeUsername\AppData\ at all.
Here is some helpful for thought about my problem to keep in mind:
That's how it should work. I am making this for my friend who is mentally disabled. He is incapable of launching and installing and doing all the work to get Minecraft running himself. Therefore I help him every day when he goes to the public library to set up and saves the game. It is kind of me to do it myself but I'm not there sometimes and he will still wants to play. That's why I need to make the USB drive for him. Keep in mind that he will not be able to fix any problems or do anything but play the game and click the "Launch" button when it get's to that step.
autorun.inf works on windows vista and up (do research into that), the built in variable USERPROFILE returns c:\users[the username of the profile, not actually whats in the brackets]

Windows Batch File for Login Shortcut

I know you can use tsdiscon.exe and tscon.exe to connect to a user account via the command line.
I want to write a simple batch file to accomplish this, so I can have a shortcut on my desktop that will quickly and easily login to another user account on this system.
I share this computer with my in-laws, and they are not very computer literate. Currently there are many steps in switching to their account, and I want to make it more streamlined for them.
It's been awhile since I've written a batch file, but I am sure there is a way to run a simple IF/THEN statement, that would check to see if the user is logged in, then either log them in with the given credentials, or switch to that users profile.
I want to know the syntax to make this happen, so I can put the .bat file on the desktop for them to click on.
Is this possible?
Logging out to your account? Suppose impossible.
Suppose your user is usr1 and you save your batch file to, say, your desktop.
Then, by running this batch file, you run it as usr1.
If your batch file contains the log out statement, it therefore ends the user account, therefore terminating the batch file, thus not being able to run the batch file successfully.
However, switching to a different account could be possible.
Try this out, if it works.
Essentially, you're doing the following:
#echo off
taskkill /IM explorer.exe /F
runas /user:COMPUTERNAME\ACCOUNTNAME explorer.exe
(Replace COMPUTERNAME and ACCOUNTNAME with the current computer name and account name (i.e. C:\Users\ACCOUNTNAME) of your in-laws account)
If that's not what you wish, then, sorry, I can't help you.
The batch script I previously mentioned here
Just give your machine a simple name customise one line in script. Test from command prompt:
fus
If it works with win10 you should be able to create two shortcuts with commands:
fus.bat 1
and
fus.bat 2

User in Windows 7 unable to modify text file

I have a software that do read and append a txt file that is located on the C:\ drive. It works perfect on Windows XP environment when a normal user in users group (not administrators) login and run the software.
However when using Windows 7 and login as a normal user in users group, I'm getting access denied even though I have modified the security of the text file to full access for that user and users group.
I have also change the ownership of the file to the users group and to the user itself.
If I change the path to the D:\ drive, there is no issue at all, but I need this to work on the C:\ drive.
I even tried changing ownership of the C:\ drive and adding write permissions as well and it didn't work.
I can't put the user into administrators group.
Thank you for your input.
Try opening the software as administrator and then open the txt file through the software. BTW where is it exactly located the txt file? (Sorry I cant comment , I havent 50 rep)

C: drive access permission in windows 7

In matlab, I used a windows standalone application. There is a line in this application that writes a file in C:\...\...\. When I run the output exe file produced from this windows standalone application, the exe doesn't write in C:\...\...\ neither tells me that there is a security issues in that partition. All the execution does is nothing. But, when I right-click and run the exe as administrator, it runs correctly.
I want to do it without right-click and run as administrator. Are there is a command in matlab that can do that?
If you create a shortcut to your application, you can go to the Properties of the shortcut, click on Advanced in the Shortcut tab, and select "Run as administrator". That way, whenever you start the application from the shortcut it will be run as an administrator.
(Disclaimer: applications really shouldn't "foul their own nest" by writing into Program Files. This is bad design.)
Starting from Vista, unprivileged processes are not allowed to write to protected folders such as Program Files, because Program Files is designed to store code and not data. However, since this limitation has not been enforced in XP, MS has provided a backward-compatibility hack in the form of Virtual Store. Now, when a program tries to write to protected folder, its output is being redirected into a dedicated folder. This way, the program still "thinks" it writes to its usual location, while in fact it writes to an unprotected location. However, when you later check the Program Files location, you might not see the file - because it's not really there.
You can find more details here: User Account Control Data Redirection.
If you are administrator, add full control permission for your username to the destination folder. You do that by right clicking on the folder, going to properties and then security tab. Then edit and add you username with Full Control rights. Then you don;t have to run the the program as an administrator.
There is no way you can elevate a process once it is started, so Matlab cannot possibly have a command for that. Just running Matlab elevated.

Find out who is locking a file on a network share

I want to known who is locking a file on a network share.
Here is the problem : the network share is on a NAS, so I can't log on. I need a tool to find out remotely who is locking the file. It is not practical to reboot the NAS every time, because there are several users.
Handle.exe, Process Explorer and PsFile seems to be limited to files on the local machine, so they don't work for me.
Just in case someone looking for a solution to this for a Windows based system or NAS:
There is a built-in function in Windows that shows you what files on the local computer are open/locked by remote computer (which has the file open through a file share):
Select "Manage Computer" (Open "Computer Management")
click "Shared Folders"
choose "Open Files"
There you can even close the file forcefully.
On Windows 2008 R2 servers you have two means of viewing what files are open and closing those connections.
Via Share and Storage Management
Server Manager > Roles > File Services > Share and Storage Management > right-click on SaSM > Manage Open File
Via OpenFiles
CMD > Openfiles.exe /query /s SERVERNAME
See http://technet.microsoft.com/en-us/library/bb490961.aspx.
PsFile does work on remote machines. If my login account already has access to the remote share, I can just enter:
psfile \\remote-share
(replace "remote-share" with the name of your file server) and it will list every opened document on that share, along with who has it open, and the file ID if I want to force the file closed. For me, this is a really long list, but it can be narrowed down by entering part of a path:
psfile \\remote-share I:\\Human_Resources
This is kind of tricky, since in my case this remote share is mounted as Z: on my local machine, but psfile identifies paths as they are defined on the remote file server, which in my case is I: (yours will be different). I just had to comb through the results of my first psfile run to see some of the paths it returned and then run it again with a partial path to narrow down the results.
Optionally, PsFile will let you specify credentials for the remote share if you need to supply them for access.
Lastly, a little known tip: if someone clicks on a file in Windows Explorer and cuts or copies the file with the intent to paste it somewhere else, that act also places a lock on the file.
If its simply a case of knowing/seeing who is in a file at any particular time (and if you're using windows) just select the file 'view' as 'details', i.e. rather than Thumbnails, tiles or icons etc. Once in 'details' view, by default you will be shown;
- File name
- Size
- Type, and
- Date modified
All you you need to do now is right click anywhere along said toolbar (file name, size, type etc...) and you will be given a list of other options that the toolbar can display.
Select 'Owner' and a new column will show the username of the person using the file or who originally created it if nobody else is using it.
This can be particularly useful when using a shared MS Access database.
The sessions are handled by the NAS device. What you are asking is dependant on the NAS device and nothing to do with windows. You would have to have a look into your NAS firmware to see to what it support. The only other way is sniff the packets and work it out yourself.
Partial answer: With Process Explorer, you can view handles on a network share opened from your machine.
Use the Menu "Find Handle" and then you can type a path like this
\Device\LanmanRedirector\server\share\
sounds like you have the same problem i tried to solve here. in my case, it's a Linux fileserver (running samba, of course), so i can log in and see what process is locking the file; unfortunately, i haven't found how to close it without killing the responsible session. AFAICT, the windows client 'thinks' it's closed; but didn't bother telling the fileserver.
Close the file e:\gestion\yourfile.dat, open by any user (/a *)
openfiles /disconnect /a * /op "e:\gestion\yourfile.dat"
more in:
http://dosprompt.info/commands/openfiles.asp

Resources