Hi i need to change folder permisions (and the contained files) from a bat file using a variable..
something like :
icacls "%dat%" /T /C /grant *S-1-1-0:(f)
but that does not seem to work as expected..
The %dat% variable works fine as the rest of the bat works for people who do not have permission issues with the folder,
the bat file i created starts off
set root=%~dp0
ECHO DRAG YOUR T.O.P FOLDER HERE
ECHO the one With "textures" folder inside.
set /p top= Then press Enter
ECHO Drag your fallout 4 Data folder here
set /p dat= then press enter
ECHO Drag your Archive2 folder here
set /p arc= then press enter
goto :choice1
(that part works fine)
It then goes to a selection of choices which also work fine.
The only issue is some people do not have permisions to modify the data folder. And the bat file needs to create folders, and move files within that folder.
I think the issue my be that:
icacls "%dat%" /T /C /grant *S-1-1-0:(f)
is settng permissions for the contents of the data folder but not the data folder itself.
its worth noting that the bat file needs to be run from inside the data folder to eliminate variables. so whilst moving it one folder up in the directory tree could allow me to use it as is and eliminate the issues of he few. It would undoubtedly cause more issues for the many.
As it stands right now everything that NEEDS to work does work, the only issue being that i cannot seem to set folder permissions on the dat folder. And i do not really want to add an extra variable unless i have too..
(i could add a "set /p new=" section to the startng sequence, but i swear i should be able to do it without that.)
Any help welcome.
i would post the entire bat file here, but i dont see it is needed. it is just a repeating list of go to: commands wich process a file inddividually.
(move a file to a created folder, extract that file to a second created folder. add extra files to the extracted files, recompress the extracted files to the same file name as the origional, moves the new file to the origional location, deletes the 2 working folders, recreates the 2 working folders with the same name, and then goes on to the next fiile)
All of that works fine. and im sure you can imagine how it is done.
-=edit=-
Just wondering if its my execution of the icals thats wrong..
would this be crrect?
icacls "%dat%" /grant:r *S-1-1-0:f /T /C
or is the 1st way i am structuring it fine?
the answer was :
icacls %dat% /grant:r *S-1-1-0:(OI)(CI)F /T /C
...
( %dat% ) being a variable directory address which was inputed earlier in the bat file.
/grant:r was needed as without :r it was giving special permissions instead of setting the actual permissions. (i thought you needed /l to do that)
/t sets the permission from the variable to all subfolders and files.
F is Full permission. (for some reason it does not need a /
/c lets the thing continue running even if there were errors.
(OI) is Object inherit.
(CI) is container inherit.
and *S-1-1-0: is the user. (that is the ID for "everyone" that way it works on any language.
"everyone" may fail on non english versions"
Related
If I have call C:\Users\user\Desktop\Main\folder\edit.bat
What is the right way to set this up this:
I want to eliminate C:\Users\user\Desktop, and make my batch universal so that I can plug in to any computer, run it from a usb, or external or simply add my batch to the desktop of anyone's computer. And run it without having to edit source or target again.
If I want it to run my edit.bat from any computer do I set it up like this
call %SystemRoot%\Main\folder\edit.bat
or
call %UserProfile%\Desktop\Main\folder\edit.bat
or
call %UserProfile%\Main\folder\edit.bat
If I understand Mofi right I can change this
call "C:\Users\user\Desktop\Main\folder\edit.bat" Original
to this
call "C:\users\%username%\Desktop\Main\folder\edit.bat" Works
and to this
call "%UserProfile%\Desktop\Main\folder\edit.bat" Works
Can anyone tell me if this is the correct
call "%~dp0\edit.bat"
If I am understanding your question correctly you want to have the .bat on the USB, plug it in, run it, and copy the files from a directory to the USB. In that case the simple script below will work. Assuming the path you choose on the client is static and does not vary, e.g. %userprofile%\desktop\ or %userprofile%\documents\.
#echo off
REM This .bat file will be on the USB drive.
set "usb=%~dp0"
set "new_path=%usb%%computername%\%username%"
if not exist "%new_path%" mkdir "%new_path%"
xcopy "%userprofile%\main\folder\files\*" "%new_path%\" /Y
if errorlevel 1 (
echo ERROR OCCURRED - %ERRORLEVEL%
pause
exit /b
)
echo Successfully copied files to "%new_path%"
timeout /t 3
exit /b
EXPLANATION
First we make a directory on the flash drive, if it doesn't already exist, so all the files are neat and organized on the USB.
if not exist checks for the directory. mkdir creates the destination directory. Pretty obvious but none-the-less.
%~dp0 defines the working directory where the .bat file is located. For more information Here is a great post.
%userprofile% environment variable is by default equal to the directory C:\users\%username%\, and %computername% expands to the computer's name.
xcopy takes the source directory and copies to our destination we created prior. /Y forces the copy and does not prompt to overwrite files.
* is a wildcard. Here is a good site for that and also everything in this script. Note that a wildcard can only be used at the end of a directory. Something like C:\users\*\desktop\*\files\* will not resolve. For something like that you would need to use for /D.
Lastly I always like to check for errors, and see if it was successful. If not we pause to make sure we see the error, or we put in a timeout /t seconds to see a success.
EDIT
Set variables for paths to account for spaces in user names, and also fixed a couple other errors in the original script that were brought to my attention.
If you want to open a file in your portable drive you can do something like this
%~d0\folder\folder\file
I have a batch file that I intend to distribute to our customers to run a software task.
We distribute them as a folder or .zip with the files inside. Inside, there is the batch files and another folder with the files needed to run the batch.
Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.
So for example: If they have the master folder on the desktop and they run it, it would need to be something like "C:\Users\Username\Desktop" to run. You would have the batch CD to that location.
But what if they run it from documents? I don't know the username, so I have to somehow have the batch find this. Any code and/or instructions would be great.
There is no need to know where the files are, because when you launch a bat file the working directory is the directory where it was launched (the "master folder"), so if you have this structure:
.\mydocuments\folder\mybat.bat
.\mydocuments\folder\subfolder\file.txt
And the user starts the "mybat.bat", the working directory is ".\mydocuments\folder", so you only need to write the subfolder name in your script:
#Echo OFF
REM Do anything with ".\Subfolder\File1.txt"
PUSHD ".\Subfolder"
Type "File1.txt"
Pause&Exit
Anyway, the working directory is stored in the "%CD%" variable, and the directory where the bat was launched is stored on the argument 0. Then if you want to know the working directory on any computer you can do:
#Echo OFF
Echo Launch dir: "%~dp0"
Echo Current dir: "%CD%"
Pause&Exit
ElektroStudios answer is a bit misleading.
"when you launch a bat file the working dir is the dir where it was launched"
This is true if the user clicks on the batch file in the explorer.
However, if the script is called from another script using the CALL command, the current working directory does not change.
Thus, inside your script, it is better to use %~dp0subfolder\file1.txt
Please also note that %~dp0 will end with a backslash when the current script is not in the current working directory.
Thus, if you need the directory name without a trailing backslash, you could use something like
call :GET_THIS_DIR
echo I am here: %THIS_DIR%
goto :EOF
:GET_THIS_DIR
pushd %~dp0
set THIS_DIR=%CD%
popd
goto :EOF
You can also do
Pushd "%~dp0"
Which also takes running from a unc path into consideration.
Try in yourbatch
set "batchisin=%~dp0"
which should set the variable to your batch's location.
I am trying to copy a file and folder from on my flash drive to the desktop of any computer I plug it into. I am somewhat familiar with the command line and batch scripts but have not used it for doing file manipulation in this way.
I originally wrote a script to test copy it from my flash drive to my desktop, however, the issue is on a new computer I won't always have the same drive letter assigned.
The code I used to test copy everything with known drive letters where the script starts in the folder on my flash drive where what I want to copy is:
copy aFile.file C:\Users\*user*\Desktop
where user is my user folder.
The main issue I am running into right now is copying things from one drive to another where it needs to go into a user profile
cd has the /D switch which specifically specifies to switch drives.
so
cd /D %userprofile%\Desktop
goes to the active user's Desktop... right where I want it.
whereas
cd %userprofile%\Desktop
does nothing because users are on a separate drive.
Is there something similar to /D for copy? The main issue is that drives letters change and user names change.
User name changes are easy to deal with because I can use %userprofile\Desktop.
If we assumed that copy made use of the same switches as cd (it doesn't) then what I basically want is:
copy /D aFile.file %userprofile%\Desktop
I am sure I am missing something relatively simple but I didn't see any switches that seemed to be what I need.
I am also open to using robocopy if that would be easier, but attributes (timestamp, owner, etc) don't matter to me so I didn't think it would benefit me immediately.
batch scripts may not be compatible with cd
try to make a script that asks two inputs.
#echo off
set /p var1="this is the file that will be moved"
set /p var2="this is the destination"
xcopy %var1% %var2%
echo copied!
pause >nul
this will ask for two inputs,first input asks for the file,second one is the file copy to
how can I organize my directories automatically?, I have a "downloads" folder, which currently contains lots of different info, for example, work related info, tv-shows, movies, etc, software, etc.
How can I automatically, maybe using some .bat execution, not sure, check for example the name of the files, or the type, and put them in the right subfolders?.
Thanks!.
You can use the move command to move files. You can also use wildcards in it.
So you could write a batch script that looks something like this:
cd C:\Users\You\Downloads
rem Excel sheets are work.
move *.xls Work
rem Reports are work.
move Report*.pdf Work\Reports
rem Pron and other viewing material ;)
move *.mp4 Private
You could run this script automatically by making it a scheduled job. Note that this script changes to the right directory first and then moves items to a relative subdirectory. This means that the directories Work, Work\Reports and Private must exist in the Downloads directory.
Of course you can expand the script to make it check and create directories as well, or you can specify different paths if you want to move the files out of the Downloads directory.
Basically, try to do it on the command line and then try to generalize those steps into your script.
This batch file will create a set of folders like .mkv .jpg .mp3 using the filetypes inside the folder, and move the files into the appropriate folders.
Launch it from your desktop and change the "c:\media" to the right folder name.
#echo off
cd /d "c:\media" && for %%a in (*) do md "%%~xa" 2>nul & move "%%a" "%%~xa" >nul
Is it possible to create a batch file to copy a folder to another location everytime I login, or when the folder is updated?
It could be written in VB or Java as well if not an easy solution.
Any ideas?
Two approaches:
When you login: you can to create a copy_my_files.bat file into your All Programs > Startup folder with this content (its a plain text document):
xcopy c:\folder\*.* d:\another_folder\.
Use xcopy c:\folder\*.* d:\another_folder\. /Y to overwrite the file without any prompt.
Everytime a folder changes: if you can to use C#, you can to create a program using FileSystemWatcher
#echo off
copy con d:\*.*
xcopy d:\*.* e:\*.*
pause
Open Notepad.
Type the following lines into it (obviously replace the folders with your ones)
#echo off
rem you could also remove the line above, because it might help you to see what happens
rem /i option is needed to avoid the batch file asking you whether destination folder is a file or a folder
rem /e option is needed to copy also all folders and subfolders
xcopy "c:\New Folder" "c:\Copy of New Folder" /i /e
Save the file as backup.bat (not .txt)
Double click on the file to run it. It will backup the folder and all its contents files/subfolders.
Now if you want the batch file to be run everytime you login in Windows, you should place it in Windows Startup menu. You find it under: Start > All Program > Startup
To place the batch file in there either drag it into the Startup menu or RIGH click on the Windows START button and select Explore, go in Programs > Startup, and copy the batch file into there.
To run the batch file everytime the folder is updated you need an application, it can not be done with just a batch file.
It's easy to copy a folder in a batch file.
#echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And you can add that batch file to your Windows login script pretty easily (assuming you have admin rights on the machine). Just go to the "User Manager" control panel, choose properties for your user, choose profile and set a logon script.
How you get to the user manager control panel depends on which version of Windows you run. But right clicking on My Computer and choosing manage and then choosing Local users and groups works for most versions.
The only sticky bit is "when the folder is updated". This sounds like a folder watcher, which you can't do in a batch file, but you can do pretty easily with .NET.
Batch file to copy folder is easy.
xcopy /Y C:\Source\*.* C:\NewFolder
Save the above as a batch file, and get Windows to run it on start up.
To do the same thing when folder is updated is trickier, you'll need a program that monitors the folder every x time and check for changes. You can write the program in VB/Java/whatever then schedule it to run every 30mins.
robocopy yourfolder yourdestination /MON:0
should do it, although you may need some more options. The switch at the end will re-run robocopy if more than 0 changes are seen.
#echo off
cls
echo press any key to continue backup !
pause
xcopy c:\users\file*.* e:\backup*.* /s /e
echo backup complete
pause
file = name of file your wanting to copy
backup = where u want the file to be moved to
Hope this helps
#echo off
xcopy ...
Replace ... with the appropriate xcopy arguments to copy what you want copied.