I'm using below command to create new folder by copying contents of the existing folder and command prompt throws Access Denied error.
Command:
C:\Users\Kundur.Rudresh>Xcopy C:\test D:\test /O /X /H /K
Please let me know do i need to give any permissions.? especially for below points.?
/O Copies file ownership and ACL information.
/X Copies file audit settings (implies /O).
If remove /O and /X from command, which works fine and folder will be created.
Add /I to your xcopy parameters. Check xcopy /? for details.
Xcopy C:\test D:\test /I /O /X /H /K
Related
I am working on a refresh project, and I am rebuilding and swapping out a lot of computers. When doing a reimage, I have a script I can run which will copy the Desktop, Favorites, and Documents from all user profiles to a network share.
What I don't have yet, is a script for hardware replacements (vs a reimage) that will transfer data directly from the old computer to the new one. As with my other script, I need it to transfer data from the Desktop, Favorites, and Documents folders for all user directories present on the old machine. If I wanted to go a step further, I might exclude specific user profiles such as Public, Default, my own profile, etc, but a more basic script would still work.
I have done some batch scripting on my own, but I'm not sure what the syntax needs to be to have it cycle through all the user profiles on a remote machine.
Here is what I have tried so far:
#echo off
Set /p remotepc=Enter remote hostname:
for /D %%D in ("\\%remotepc%\USERS\*") do (xcopy \\%remotepc%\Users\%%~fD\Desktop "C:\Users\%%~nxD\Desktop" /H /E /Y /K /I /R /C)
for /D %%D in ("\\%remotepc%\USERS\*") do (xcopy \\%remotepc%\Users\%%~fD\Documents "C:\Users\%%~nxD\Documents" /H /E /Y /K /I /R /C)
for /D %%D in ("\\%remotepc%\USERS\*") do (xcopy \\%remotepc%\Users\%%~fD\Favorites "C:\Users\%computername%\%%~nxD\Favorites" /H /E /Y /K /I /R /C)
pause
This question already has answers here:
Why does the command XCOPY in batch file ask for file or folder?
(11 answers)
Closed 3 months ago.
My batch script xcopy is still asking F = file, D = directory confirmation even though I have added /F in the script, the log is showing as below.
Please help on how to avoid asking confirmation.
Script:
net use p: /delete
net use p: "\\200clan\F_Drive" /USER:adm /PERSISTENT:NO-1111
set source=%1
set target=p:/%2
echo %source% %target%
xcopy /S /I /Q /Y /F "%source%" "%target%"
Log:
C:\test\foldera>xcopy /S /I /Q /Y /F "C:/test/folder1/folder2/logs/154/compareReport_177.html" "p:/Services/WSDLCompare/177_20151116/compareReport_177.html"
Does P:\Services\WSDLCompare\177_20151116\UIReport_177.html specify a file name
or directory name on the target
(F = file, D = directory)?
The /I switch (not /F as you mentioned in your question) prevents xcopy from asking whether the destination is a file or a directory only if multiple source files are given, so if the source is a directory, or if wildcards ? or * are used. If the destination already exists, such prompt does never appear.
There are the following scenarios (depending on the provided values of %source% and %target%):
a single source file, the destination is a file:
the /I switch is useless, so you need to pipe F into the xcopy command line:
echo F|xcopy /S /Q /Y /F "%source%" "%target%"
provided that the /Y switch is given (to force overwriting), you could also create the target file in advance (empty file):
>> "%target%" rem/
xcopy /S /Q /Y /F "%source%" "%target%"
a single source file, the destination is a directory:
the /I switch is useless too; you can pipe D into the xcopy command line:
echo D|xcopy /S /Q /Y /F "%source%" "%target%"
or you can simply append a \ to the destination:
xcopy /S /Q /Y /F "%source%" "%target%\"
although this causes trouble when %target% specifies the current directory of a drive like D: for instance, because D: means the current directory of this drive whereas D:\ means the root directory of it;
or you create the destination directory in advance:
2> nul mkdir "%target%"
xcopy /S /Q /Y /F "%source%" "%target%"
the 2> nul portion suppresses the error message in case the directory already exists;
multiple source files, the destination is a file:
this is usually a senseless situation, because you tell xcopy to copy each source file to the same destination file, thus attempting to overwrite it;
multiple source files, the destination is a directory:
the /I switch makes sense here:
xcopy /S /I /Q /Y /F "%source%" "%target%"
the pipe option also works here:
echo D|xcopy /S /Q /Y /F "%source%" "%target%"
so does appending a \ to the destination (regarding the limitation as mentioned above):
xcopy /S /Q /Y /F "%source%" "%target%\"
or you create the destination directory in advance:
2> nul mkdir "%target%"
xcopy /S /Q /Y /F "%source%" "%target%"
Conclusion
The most flexible and secure solution is to pipe the desired selection (F or D) into the xcopy command line. (Note that the query is locale-dependent.)
Supplement
There are some minor issues in your code fragment I want to mention here:
you should generally use the \ as a path separator as this is the Windows standard character for that purpose (although / works too in most cases);
there is -1111 appended to your second net use command line; if this constitutes the password for the resource, it should be moved before the /USER option; otherwise just remove it;
your set command lines introduce problems with some special characters (like &, ^, (, )); to avoid such, state set "source=%~1" and set "target=p:/%~2"; the ~ removes potential surrounding "" from the arguments (which are required if they contain SPACE, ,, ;, =);
Here is the code with the all of the above things reworked:
net use P: /DELETE
rem supposing `-1111` constitutes the password for the resource:
net use P: "\\200clan\F_Drive" -1111 /USER:adm /PERSISTENT:NO
set "source=%~1"
set "target=P:\%~2"
echo "%source%" "%target%"
rem supposing the destination is a directory:
echo D|xcopy /S /I /Q /Y /F "%source%" "%target%"
rem actually you do not need any interim variables:
REM echo D|xcopy /S /I /Q /Y /F "%~1" "P:\%~2"
Put a \ behind the target to suppress the question and specify it as a folder:
xcopy src dest\
Put a * behind the target to suppress the question and specify it as a file:
xcopy src dest*
Note: The second command is a hack that relies on wildcard matching (will match any item starting with "dest") so use it at your own risk!
xcopy doesn't know the target is a directory. You clarify this by putting a backslash at the end:
xcopy /S /I /Q /Y /F "%source%" "%target%\"
When copying a single file with XCOPY, there is no option to indicate if the destination is a filename or a directory (with the filename defaulting to that of the source file).
In such cases XCOPY will prompt with a (locale specific) message like:
C:> xcopy foo.txt bar.txt
it prompts
Does foo.txt specify a file name or directory name on the target (F = file, D = directory)?
Adding a wildcard (*) to the end of the destination will suppress this prompt and default to copying as a file:
C:> xcopy foo.txt bar.txt*
1 File(s) copied
This requires the source and target file extensions to be the same length, typically 3 characters.
for more information: https://ss64.com/nt/xcopy.html
Removing the destination filename will suppress the message. This works for me!
I use:
xcopy /S /I /Q /Y /F "%source%" "%target%"
It works.
Try:
echo F>xcopy_answer.tmp
xcopy xcopy /Q/Y "%source%" "%target%"<xcopy_answer.tmp
How do I delete files or folders recursively on Windows from the command line?
I have found this solution where path we drive on the command line and run this command.
I have given an example with a .svn file extension folder:
for /r %R in (.svn) do if exist %R (rd /s /q "%R")
The other answers didn't work for me, but this did:
del /s /q *.svn
rmdir /s /q *.svn
/q disables Yes/No prompting
/s means delete the file(s) from all subdirectories.
Please execute the following steps:
Open the command prompt
Change directory to the required path
Give the following command
del /S *.svn
You can use this in the bat script:
rd /s /q "c:\folder a"
Now, just change c:\folder a to your folder's location. Quotation is only needed when your folder name contains spaces.
RMDIR path_to_folder /S
ex. RMDIR "C:\tmp" /S
Note that you'll be prompted if you're really going to delete the "C:\tmp" folder. Combining it with /Q switch will remove the folder silently (ex. RMDIR "C:\tmp" /S /Q)
For file deletion, I wrote following simple batch file which deleted all .pdf's recursively:
del /s /q "\\ad1pfrtg001\AppDev\ResultLogs\*.pdf"
del /s /q "\\ad1pfrtg001\Project\AppData\*.pdf"
Even for the local directory we can use it as:
del /s /q "C:\Project\*.pdf"
The same can be applied for directory deletion where we just need to change del with rmdir.
If you want to delete a specific extension recursively, use this:
For /R "C:\Users\Desktop\saleh" %G IN (*.ppt) do del "%G"
Use the Windows rmdir command
That is, rmdir /S /Q C:\Temp
I'm also using the ones below for some years now, flawlessly.
Check out other options with: forfiles /?
Delete SQM/Telemetry in windows folder recursively
forfiles /p %SYSTEMROOT%\system32\LogFiles /s /m *.* /d -1 /c "cmd /c del #file"
Delete windows TMP files recursively
forfiles /p %SYSTEMROOT%\Temp /s /m *.* /d -1 /c "cmd /c del #file"
Delete user TEMP files and folders recursively
forfiles /p %TMP% /s /m *.* /d -1 /c "cmd /c del #file"
For completely wiping a folder with native commands and getting a log on what's been done.
here's an unusual way to do it :
let's assume we want to clear the d:\temp dir
mkdir d:\empty
robocopy /mir d:\empty d:\temp
rmdir d:\empty
You could also do:
del /s /p *.{your extension here}
The /p will prompt you for each found file, if you're nervous about deleting something you shouldn't.
The Windows Command Processor cmd.exe has two internal commands for deletion of files and folders:
The command DEL is for the deletion of files with usage help output on running in a Windows command prompt window either help del or del /?.
The command RMDIR or with shorter name RD is for removal of directories with usage help output on running in a Windows command prompt window either help rmdir or rmdir /? or help rd or rd /?.
Deletion of all *.svn files in an entire folder tree
There can be used in a Windows command prompt window or a Windows batch file the following command to delete really all files of which long or short 8.3 file name is matched by the wildcard pattern *.svn in the directory %USERPROFILE%\Projects or any of its subdirectories:
del /A /F /Q /S "%USERPROFILE%\Projects\*.svn" >nul 2>&1
The usage of option /A to match all files independent on the file attributes replaces the implicit default /A-H to ignore hidden files. So even files with hidden attribute are deleted by this command because of using the option /A. Files matched by wildcard pattern *.svn with hidden attribute set are ignored on not using the option /A.
The option /F forces a deletion of files with file extension .svn which have the read-only attribute set. There would be output the error message Access is denied. if a *.svn file has the read-only attribute set and the option /F is not used on running the command DEL.
The quiet option /Q prevents the user confirmation prompt Are you sure (Y/N)?.
The option /S results in searching not only in the specified directory, but also in all its subdirectories including those with hidden attribute set even on not using option /A for files of which long or short 8.3 name is matched by the wildcard pattern *.svn.
The two redirections >nul and 2>&1 result in redirecting the list of deleted files output to handle STDOUT (standard output) and the error messages output to handle STDERR (standard error) to the device NUL to suppress every output.
There are deleted also hard links and symbolic links matched by the wildcard pattern *.svn on using this command, but not the files linked to on having a file name not ending with .svn or being in a different directory tree.
Files matched by the wildcard pattern *.svn currently opened by a process (program/application) with using shared access permissions to deny all other processes to delete the file as long as being opened by this process are not deleted by this command. File system permissions can result also in files not being deleted by this command.
Deletion of all *.svn folders in an entire folder tree
There can be used in a Windows command prompt window the following command to remove really all folders matching in long or short 8.3 folder name the wildcard pattern *.svn in the directory %USERPROFILE%\Projects and all its subdirectories:
for /F "delims=" %I in ('dir "%USERPROFILE%\Projects\*.svn" /AD /B /S 2^>nul') do #rd /Q /S "%I" 2>nul
The same command line for usage in a batch file containing #echo off at top is:
for /F "delims=" %%I in ('dir "%USERPROFILE%\Projects\*.svn" /AD /B /S 2^>nul') do rd /Q /S "%%I" 2>nul
There is executed on more cmd.exe in background with option /c and the command line specified between ' as additional arguments to run in background the Windows Command Processor internal command DIR to search
in the specified directory %USERPROFILE%\Projects
and in all its subdirectories because of option /S
for just directories because of using the option /AD which includes also junctions and symbolic directory links
matching the wildcard pattern *.svn.
The file system entries (= directory names) matched by these criteria are output in bare format because of option /B with full path because of option /S to handle STDOUT of the background command process without surrounding " even on full directory name containing a space or one of these characters &()[]{}^=;!'+,`~. The error message output by DIR on not finding any name matching these criteria is redirected to device NUL to suppress it.
The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when the Windows Command Processor parses this command line before executing the command FOR which executes the embedded dir command line with using a separate command process started in background.
The output list of directory names with their full paths to handle STDOUT is captured by cmd.exe processing the batch file and processed by FOR after started cmd.exe closed itself.
The FOR /F option delims= defines an empty list of string delimiters which results in each entire directory name is assigned completely one after the other to the specified loop variable I.
The command RD is executed to delete quietly because of option /Q the directory with all files and all subdirectories because of option /S.
There are deleted also junctions (soft links) and symbolic directory links matched by the wildcard pattern *.svn on using this command, but not the directories linked to on having a directory name not ending with .svn or being in a different directory tree.
A directory matched by the wildcard pattern *.svn in which a file is currently opened by a process (program/application) with using shared access permissions to deny all other processes to delete the file as long as being opened by this process is not deleted by this command and of course also no directory above the directory containing the file which cannot be deleted at the moment. File system permissions can result also in directories not being deleted by this command. Windows prevents by default also the deletion of a directory which is the current working directory of any running process.
Other useful information regarding to deletion of files and folders
The directory path %USERPROFILE%\Projects\ can be removed completely or replaced by .\ in the commands above to delete the files and folders matching the wildcard pattern *.svn in the current directory of the Windows Command Processor process which executes the commands.
The directory path %USERPROFILE%\Projects\ can be replaced by %~dp0 to delete the files and folders matching the wildcard pattern *.svn in the directory of the batch file on using the command lines above in a batch file independent on which directory is the current directory on execution of the batch file.
The directory path %USERPROFILE%\Projects\ can be replaced also by a relative path. Please read the Microsoft documentation about Naming Files, Paths, and Namespaces for more details about relative paths.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
del /?
dir /?
for /?
rd /?
Run mklink /? for help on how to create file and directory links explained very well by MKLink.
See also:
Microsoft documentation for the Windows commands
SS64.com - A-Z index of Windows CMD commands
For hidden files I had to use the following:
DEL /S /Q /A:H Thumbs.db
dir -Recurse *.[extension] |del
dir /b %temp% >temp.list
for /f "delims=" %%a in (temp.list) do call rundll32.exe advpack.dll,DelNodeRunDLL32 "%temp%\%%a"
It worked for me
del /s /q "dir_name"
I'm trying to find a way using, hopefully, a batch file to copy a modified user profile to the default user profile so that any user that logs in to the computer will adopt the profile setup. We're using a utility called pGina to call the default profile for a user logged in through a RADIUS server, as opposed to having the user log in to the domain directly. Currently, we're trying to use robocopy for this.
cd C:\Users\
rmdir Default /s /q
robocopy /COPY:DAT /R:5 /E C:\Users\user1 C:\Users\Default
pause
Does anyone have any idea how to successfully copy the profile? Thanks in advance.
This is what I found when searching for an answer to your problem
rem delete old Default User profile
RD /s /q "%systemdrive%\Profiles\Default User"
rem copy current user profile to default user profile
xcopy "%USERPROFILE%\*.*" "%systemdrive%\Profiles\Default User" /e /c /I /H /R /K /Y
rem delete non-need some files
del /f /q "%systemdrive%\Profiles\Default User\*.*"
rem set default attributes
attrib +h "%systemdrive%\Profiles\Default User"
rem registry trick
rem no directly copy locked ntuser.dat file
rem use reg tools to save current user registry to file
reg save HKCU "%systemdrive%\Profiles\Default User\ntuser.dat"
rem set default attributes to hive file
attrib +H +S "%systemdrive%\Profiles\Default User\ntuser.dat"
I need to know how to program a batch file to prompt user for source and destination paths.
Once paths are entered by the user, the application will copy all files/folders
from [SOURCE] to [DESTINATION]
The solution should not require any user input beyond source and destination paths
It should use a system of variables to contain the paths
It should log copy process
So far i have :
xcopy "c:\source" "c:\destination" /e /h /k /o
pause
Set command can take input from user: set /p source=Enter Source path.
Read help (set /?) for more information.
set /p source=Enter Source path
xcopy %source% "c:\destination" /e /h /k /o