The following code deletes everything in Desktop, Download and Documents including the folders themselves. Is there any way to delete all the folders inside, but not the folder itself?
echo cmd delete all files in folder
del C:\Users\shmuel_admin\Downloads\*.*" /s /f /q
del C:\Users\shmuel_admin\Desktop\*.*" /s /f /q
del C:\Users\shmuel_admin\Documents\*.*" /s /f /q
rd /s /q "C:\Users\shmuel_admin\Desktop"
rd /s /q "C:\Users\shmuel_admin\Downloads"
rd /s /q "C:\Users\shmuel_admin\Documents"
PowerShell.exe -NoProfile -Command Clear-RecycleBin -Confirm:$false
echo Done!
You're using PowerShell to empty out the Recycle Bin; why not use PowerShell for the entire job?
Write-Host "Deleting all files and subfolders..."
Get-ChildItem -Path C:\Users\shmuel_admin\Downloads -Recurse | Remove-Item
Get-ChildItem -Path C:\Users\shmuel_admin\Desktop -Recurse | Remove-Item
Get-ChildItem -Path C:\Users\shmuel_admin\Documents -Recurse | Remove-Item
Clear-RecycleBin -Confirm:false
Write-Host "Done!"
...and that's it!
Related
How to implement file deletion in Powershell without putting it in recycle.bin?
The current command moves files older than 30 days to the trash.
$int1 = 30
New-PSDrive -Name "E" -PSProvider "FileSystem" -Root "\\Computer\c`$\Folder" -Persist
#FORFILES /p E:\ /s /m *.* /d -$int1 /c "CMD /c del /Q #FILE"
Remove-Item Deleting files without moving them to the Recycle Bin
I'm looking for a command using cmd.exe (Win 10) that will list all files in a folder and its sub-folders, alphabetically, irrespective of the paths, and that will show the filenames only (no paths).
The commands that I'm familiar with (including, for example, "dir ..\samplefolder /b /s /A-D /o:n > filelist.txt") all include the paths in the output, and so are not what I'm looking for.
Thank you.
(for /r "c:\startfolder" %%A in (*) do echo %%~nxA)|sort
(this is batch file syntax; for use directly on the command line, replace every %% with just %)
for /r loops recursively over all (non-hidden) files.
%%~nxA shows name and extension only (if you want just the name without extension, use %%~nA)
See for /? for more information on those modifiers.
If the machine is on the current PowerShell 5 or higher, you could use:
(Get-ChildItem -Recurse -File -Path '..\samplefolder').Name |
Sort-Object |
Out-File -PSPath 'filelist.txt' -Encoding ascii
In a .bat file script.
>"filelist.txt" powershell -NoLogo -NoProfile -Command ^
"(Get-ChildItem -Recurse -File -Path '..\samplefolder').Name | Sort-Object"
If the machine does not have a current PowerShell, it should be upgraded or use:
>"filelist.txt" powershell -NoLogo -NoProfile -Command ^
"(Get-ChildItem -Recurse -Path '..\samplefolder'|" ^
"Where-Object { -not $_.IsContainer}).Name |" ^
"Sort-Object"
Trying to use Powershell to backup some large dir -newbie
I can't make this line to work
"C:\Robocopy\RoboCopy.exe" $source $destination "/E /R:10 /W:5 /V /ETA"
Robocopy at best (depending on the " i put here or there..) is executed but it's its GUI that is launched (and nothing more is done).
There's no issue with the $dest and $source (I manage to log into a txt file and his is working)
Thank you
Use this:
& "C:\Robocopy\RoboCopy.exe" $source $destination /E /R:10 /W:5 /V /ETA
The & (call) operator is required if you want PowerShell to run a quoted string as a command.
In this specific case, the quotes are not needed because the executable's path and filename don't contain spaces, so you can just write this instead:
C:\Robocopy\RoboCopy.exe $source $destination /E /R:10 /W:5 /V /ETA
But is robocopy.exe really sitting in C:\Robocopy? Do you have that directory name? Robocopy.exe is a tool that comes with the OS and should already be in the path. Why not just this?
robocopy $source $destination /E /R:10 /W:5 /V /ETA
I'm working in Sterling B2B Integrator and I have to create a Business Process to collect only the files from "yesterday" (the previous date) The problem is that B2Bi doesn't have a service to do that and the colection directory has over than 7000 files, so I can't use a GetDocInfo service to collect the dates into tags because the Sterling may colapse.
So, I decided to use the Command Line Adapter to invoke a script that would do that for me. The problem is that the script doesn't work either:
set var1=%1 /* UNC File Path */
set var2=%2 /* Source directory */
set var3=%3 /* "yesterday" date */
set var4=%4 /* save the list of files into a .txt*/
set var5=%5 /* copy the files from yesterday into this directory */
PUSHd **%var1%** &
forfiles /p **%var2%** /s /C " cmd /c echo #path #FDATE | findstr /m **%var3%**" > %var4% &
for /f %%a in (**%var4%**) do copy %%a **%var5%** &
Function: The script should collect the files from yesterday and save them into a specific directory.
Example:
PUSHd "\\emea\e801\Public" &
forfiles /p _AppData\CAMS\PDFS\Digital\CertificadoCancelado /s /C " cmd /c echo #path #FDATE | findstr /m "27/07/17"" > _Shared\_AppData\MFT\BackupSterling\temp_puente_PRO\Lista_DIGCRT02\ficherosAyer.txt &
for /f %%a in (_Shared\_AppData\MFT\BackupSterling\temp_puente_PRO\Lista_DIGCRT02\ficherosAyer.txt) do copy %%a _Shared\_AppData\MFT\BackupSterling\temp_puente_PRO\Lista_DIGCRT02\DIGCRT02 &
Why is this script not working?
The script is not working because it is not syntactically correct. What are the asterisks doing around the variable names.
Here is a brief PowerShell script that is the core of what you need to do. It needs to have a Parms() block. When you are satisfied that it will copy the files correctly, remove the -WhatIf from the Copy-Item command.
Please note that this does not maintain the subdirectory structure from the src_dir. This will not work well if you have selected files with the same name in different subdirectories.
$src_dir = 'C:\src\t' #var2
$the_date = '2017-07-21' #var3
$log_file = 'C:\src\xxx' #var4
$dest_dir = 'C:\src\xxx' #var5
if (Test-Path $log_file) { Remove-Item $log_file }
Get-ChildItem -Path $src_dir -File -Recurse |
ForEach-Object {
if ((Get-Date $_.LastWriteTime -Format yyyy-MM-dd) -eq $the_date) { $_.FullName }
} |
Tee-Object -FilePath $log_file -Append |
Copy-Item -Destination $dest_dir -WhatIf
If you -must- do this from a .bat script, put the script above into a filename with a .ps1 extension such as Move-FilesDated.ps1. Then, call it from the .bat script.
powershell -NoProfile -File "Move-FilesDated.ps1"
I'm drafting a powershell script to manually backup some DC's. The backups need to be moved from one folder to another and I am using Get-ChildItem -Filter "Backup*" to select the backup files, then robocopy to move them.
It works but I would like to add in a trap and custom error message "Error Copying Local Backup from XX to SystemStateBackup" if the source or destination path is incorrect. I would also like to pipe any errors to a log file.
The original with no trap is
Get-ChildItem f:\WindowsImageBackup\ITUPW-PRODDC7 -Filter "Backup*" -Name | ForEach-Object {
robocopy "f:\WindowsImageBackup\ITUPW-PRODDC7\$_" "f:\WindowsImageBackup\ITUPW-PRODDC7\SystemStateBackup\$_" /Z /S /MOVE /njh /njs /ndl /nc /ns /np /nfl}
The trap will work if I only use part of the code
Trap {"Error Copying Local Backup from XX to SystemStateBackup";Continue} Get-ChildItem f:\WindowsImageBackup\ITUPW-PRODDC7 -Filter "Backup*" -Name -ea "Stop"
But it won't work this way
Trap {"Error Copying Local Backup from XX to SystemStateBackup";Continue} Get-ChildItem f:\WindowsImageBackup\ITUPW-PRODDC7 -Filter "Backup*" -Name | ForEach-Object {
robocopy "f:\WindowsImageBackup\ITUPW-PRODDC7\$_" "f:\WindowsImageBackup\ITUPW-PRODDC7\SystemStateBackup\$_" /Z /S /MOVE /njh /njs /ndl /nc /ns /np /nfl} -ea "Stop"
It results in the following powershell error
Get-ChildItem : Cannot find path 'F:\WindowsImageBackup\ITUPW-PRODDC' because it does not exist.
At line:1 char:88
+ Trap {"Error Copying Local Backup from XX to SystemStateBackup";Continue} Get-ChildItem <<<< f:\WindowsImageBackup\I
TUPW-PRODDC -Filter "Backup*" -Name | ForEach-Object { robocopy "f:\WindowsImageBackup\ITUPW-PRODDC7\$" "f:\WindowsIma
geBackup\ITUPW-PRODDC7\SystemStateBackup\$" /Z /S /MOVE /njh /njs /ndl /nc /ns /np /nfl} -ea "Stop"
+ CategoryInfo : ObjectNotFound: (F:\WindowsImageBackup\ITUPW-PRODDC:String) [Get-ChildItem], ItemNotFoun
dException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Any advice would be greatly appreciated.
Thank you for you time.
Amelia - A sys admin who drew the short straw
You are applying the -ea "Stop" to the foreach rather than to the Get-ChildItem. Move it before the pipe |
For the robocopy, you might want to use $? or $lastexitcode (within the foreach-object) to see if it ran fine.