How can I fix my batch file??
When I run the command in Windows 10 1903, it run totally fine. But when I run it in Windows 10 2004, the batch keep deleting file that's in the batch folder.
Here is the script (not full) :
ipconfig /flushdns
cd %windir%/temp
powershell ri * -recurse -force >NUL
cd %temp%
powershell ri * -recurse -force >NUL
this is the line that keep deleting my file in Windows 10 2004
Can someone plz help me, I already try to solve it. But I didn't find a way out
Rather than changing directory, directly pass the path of the directory to the ri command.
ipconfig /flushdns
powershell ri %windir%/temp/* -recurse -force >NUL
powershell ri %temp%/* -recurse -force >NUL
The Temp directory you're trying to empty is inside the protected %SystemDrive%\Windows location. In order for it to be emptied, you must, in the first instance, have permission to do that, and generally for that you should be running your script elevated, i.e. 'Run as administrator'.
Also, I would have thought it would be quicker to do this directly with native cmd.exe commands, rather than calling out to an external utility like %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe:
#"%SystemRoot%\System32\ipconfig.exe" /FlushDNS
#PushD "%SystemRoot%\Temp" && (RD /S /Q . 2> NUL & PopD)
#PushD "%Temp%" && (RD /S /Q . 2> NUL & PopD)
Related
a user of mine is having problems with syncing cloud to local workstation. he is having lots of folders in a deep (10+) folder structure pattern and every folder is containing at least three dots ("xy.asdf.qwer $generic descriptor").
so now there is big foobar. online everything works fine, new files can be created and edited as expected. but the synced files keeps crashing on windows 10 enterprise and explorer warns with "bad foldername" and stuff. so i allowed long folder names (260 chars max.... seriously?) via GPO for user and i thought "well done. problem solved" ... but no... synced local files are still having trouble... so i allowed same option via regedit. same same. explorer keeps complaining.
so i decided to change folder names and remove the dots and replace them with spaces. so folder "xy.asdf.qwer $generic descriptor" will be "xy asdf qwer $generic descriptor". user says it's fine so i wrote a batch to rename folders. sry, my powershell is really bad. but if u know a solution in powershell it is appreciated and fine as well.
#echo off
setlocal enabledelayedexpansion
set "leer= "
set "punkt=."
call :rekursion
goto :eof
:rekursion
for /D %%d in (*) do (
set name=%%~nxd
ren "%%d" "!name:%punkt%=%leer%!"
cd %%d
call :rekursion
cd ..
)
exit /b
batch is working fine, but keeps throwing errors and telling me "not allowed" ... what is wrong with it? why is it working but complaining? what am i missing? all rights are set proper (full access...)
[sry for bad English...]
This PowerShell script will do what you want. If you are on a supported Windows platform, PowerShell will be available.
I would not want SPACE characters in directory or file names, but that is your choice. When you are satisfied that the names would be changed correctly, remove the -WhatIf from the Rename-Item command.
#Requires -Version 3
$BaseDir = 'C:\src\t'
Get-ChildItem -Recurse -Directory -Path $BaseDir |
Sort-Object -Descending -Property FullName |
Where-Object { $_.Name -match '\.' } |
ForEach-Object {
$NewName = $_.Name -replace '\.',' '
Rename-Item -Path $_.FullName -NewName $NewName -WhatIf
}
The order is the issue. You are renaming the folder BEFORE you try to change directories. It works because after you rename the folder, the loop will pick up the new folder name, but is much less efficient. I was seeing errors on the 'cd' line. This works for me and is a bit simpler:
#echo off
setlocal enabledelayedexpansion
set "leer= "
set "punkt=."
:rekursion
for /D %%d in (*) do (
pushd %%d
call :rekursion
popd
set name=%%~nxd
ren "%%d" "!name:%punkt%=%leer%!"
)
So i'm currently trying to delete a bunch of subfolders that were created by a program, on a recurring basis because it will recreate the subfolders. I know how to schedule the batch file, right now my issue is how to efficiently create the batch file to begin with. To clarify, say i have a directory named D:\IGSG. There are 250 folders within D:\IGSG, and within each one of those is a folder named D:\IGSG\F252341\arg. I want to delete all of the \arg folders, from all ~250 F folders I have, without deleting the other files inside the F folders. Right now the only way to do it that I know of would be to have a batch that goes
cd D:\IGSG\F252341
del /Q arg
and to repeat those lines for every subfolder, but typing out those folder names for every folder in there would be tedious, especially considering new ones are created from time to time. So i'm looking for a way with a batch to delete the subfolder of a subfolder, without deleting other files, if that makes sense.
On the cmd line for /d /r X:\path in combination with a wildcard will enumerate all folders names arg the appended .? meets this requirement:
for /d /r D:\IGSG %A in (arg.?) do #echo rd /s /q "%A"
if the output looks right remove the echo.
In a batch file double the %-signs -> %%A
#Echo off
for /d /r D:\IGSG %%A in (arg.?) do echo rd /s /q "%%A"
One of the rare cases where batch/cmd line can compete with PowerShell.
This PowerShell script will recurse through the subdirectory structure and delete all files and subdirectories inside arg directories. Be sure to change the location of the IGSG directory to yours. When you are satisfied that the correct files will be deleted, remove the -WhatIf from the Remove-Item cmdlet.
Get-ChildItem -Directory -Recurse -Path 'C:\src\t\delsubs\IGSG\F*' -Filter 'arg' |
Remove-Item -Path {(Join-Path -Path $_.FullName -ChildPath '*')} -Recurse -Force -WhatIf
If you need to run it from a cmd.exe shell, put the code above into a file named 'dodel.ps1' and run using:
powershell -NoProfile -File .\dodel.ps1
I am trying to write a bat file that will accomplish the following:
psexec \\host1 cmd
d: #to change the remote server's drive
cd\
dir /s/b "file1" #searches for this file in host1 in its d: drive
How can I go about doing this
If you are on a current day Windows system, you can invoke the command on a remote computer using PowerShell from within your cmd .bat script. No need for psexec. The remote machine does need to be setup for PowerShell remoting. Get-Help about_Remote
powershell -NoProfile -Command "invoke-command HOST01 { cmd /C dir /S /B D:\file1 }"
If you are running in PowerShell:
invoke-command HOST01 { cmd /C dir /S /B D:\file1 }
Of course, in PowerShell you might as well use PowerShell cmdlets.
icm HOST01 { gci -n -rec D:\file1 }
-or-
Invoke-Command HOST01 { Get-ChildItem -Name -Recurse D:\file1 }
psexec \\host1 cmd /c "d: & cd\ & dir /s/b file1"
or simply
psexec \\host1 cmd /c "dir /s/b d:\file1"
The console in which the cmd is executed is automatically closed when the command finishes executing, so you won't actually see the result. You could leave cmd running (and console along with it) by using /k instead of /c, but that doesn't make much sense either. You appear to have an XY problem.
I am having an application folder with sub-folders and thousands of files in it. I want to write a batch script which lists all the files which DO NOT contain particular text, say SAMPLE_TEXT and redirect output to a file. Please help with the script.
Inspired by http://tobint.com/blog/powershell-selecting-files-that-dont-contain-specified-content/ this powershell worked well for me;
Get-ChildItem -include *.sql -recurse | ForEach-Object { if( !( select-string -pattern "USE " -path $_.FullName) ) { $_.FullName}} > FilesMissingUse.txt
In my case I was searching for database scripts (.sql files) which were missing "USE " string.
This may help you - launch it in the top level folder.
#echo off
(for /r %%a in (*) do find "SAMPLE_TEXT" "%%a" >nul || echo %%a)>file.log
#echo off
findstr /S /M /V "SAMPLE_TEXT" *.* > output.txt
With grep you can use
grep -L Font *.pdf > list_of_files.txt
The -L switch returns only files that do not contain the string "Font."
I'm trying to execute a bat file on a different server that copies files to several other servers. I can get to the bat file and execute it just fine. But the commands in the bat file don't run. I know the bat file executes because I added an echo > text.txt statement, and that worked.
Here is what I have:
Invoke-WmiMethod -class Win32_process -name Create -ComputerName $serverName -Credential $Credentials -ArgumentList "cmd /c D:\startBat.bat"
Here is the bat file on the other server
d:
cd \path\path2
echo "asdf" > text.txt
copy /Y file.zip \\serv5\e$\temp\temp1
copy /Y file.zip \\serv4\e$\temp\temp1
copy /Y file.zip \\serv3\e$\temp\temp1
copy /Y file.zip \\serv2\e$\temp\temp1
copy /Y file.zip \\serv1\e$\temp\temp1
copy /Y file2.zip \\serv5\e$\temp\temp1
copy /Y file2.zip \\serv4\e$\temp\temp1
copy /Y file2.zip \\serv3\e$\temp\temp1
copy /Y file2.zip \\serv2\e$\temp\temp1
copy /Y file2.zip \\serv1\e$\temp\temp1
I'm at a loss as to what's going on here. I don't think it's a permission thing because I can get a service as a wmi object just fine and start, stop the service all I want. So I'm fairly certain my credentials are good.
thanks!