I have been banging my head with this small script to search a delete a file from the remote host. However, I have been able to write to seperate scripts, both to delete and to find a file.
However, now I am trying to merge both and I am unable to use the for loop in the same line as the line which searches the file.
Psexec #IPlist.txt -u ad -p P#$$vv0rCL cmd /c (^WHERE /r D:\ %file%>res.txt
the above line helps to search the file, and below does the deletion part.
del /f /A:H /S /Q "D:\1\e.txt"
Now, since I want to delete all the files, found and stored in rex.txt, I need to run a for loop on this file.
Here is what I am trying to do
Psexec #IPlist.txt -u ad -p P#$$vv0rCL cmd /c (^WHERE /r D:\ %file%>res.txt ^& FOR /f "delims=" %%X IN (res.txt) DO ( del /f /A:H /S /Q "D:\1\e.txt")
but this gives error cmd exit with error code 1, and if I take for loop on next line, then the code doesn't work on remote pc.
Can some one guide please?
escape the ) in (res.txt) as ^)
Related
I have an application which saves files to a FTP folder that I sync to my PC which contains multiple JPG and MP4 files, named in the following format:
ARC20170510151547.jpg
ARC20170510151549.mp4
What I'm trying to do is:
Copy the files from my FTP to my PC
Sort the files into folders based on the day they were created
Delete files from the FTP older than 14 days
Delete files from the PC older than 1 month
Using WinSCP to connect to FTP with the following code, I am able to download all the files to my local drive:
"c:\program files (x86)\winscp\winscp.com" /ini=nul /command ^
"open ftp://[username]:[password]#[ipaddress]/" ^
"synchronize local f:\[localpath]\ /[remotepath]/ " ^
"exit"
Then I need to sort the files. Here is where I am stuck. I'm think I know the commands but I am unsure how to use the 'tokens' and 'delims' to get it to work how I want.
#echo
for %%a in (*.*) do (
echo processing "%%a"
for /f "tokens=1 delims=" %%a in ("%%~nxa") do (
md "%%b-%%c" 2>nul
move "%%a" "%%b-%%c" >nul
)
)
pause
As I know the filename format isn't going to change, one thing I have considered is adding some special characters to the filename, maybe using the 'ren' command. I could then use those special characters as search delims but, again, I'm struggling how to best proceed.
Removing local files older than 30 days is easy using the following script
forfiles -p "f:\[localpath]" -s -m *.* -d <number of days> -c "cmd /c del #path"
However, the WinSCP 'RM' command I am using doesn't appear to be working. It returns an error "no file matching '*<14D' found"
"rm /[filepath]/*<14D" ^
Any help, advice and guidance would be very gratefully received!
Since there is no delimiter between the date elements you need substrings,
substrings do only work with normal variables and in a (code block) you need delayed expansion
It's unclear if you want folders with year-month or month-day, select the proper part in the batch and comment/uncomment the Rem:
With extensions enabled md can create the structure YY-MM\DDin one step.
So you can move directly to that folder.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
for %%a in (ARC*.*) do (
echo processing "%%a"
Set File=%%~nA
Set YY=!File:~3,4!
Set MM=!File:~7,2!
Set DD=!File:~9,2!
Rem YY-MM\DD
md "!YY!-!MM!\!DD!" 2>nul
move "%%a" "!YY!-!MM!\!DD!" >nul
)
pause
#LotPings! Thank you for the script. This does almost what I want it to do. I have modified the script as below which moves the files into folders based on the Day.
So, each day, maybe 100 - 200 files are generated. So I do not mind having a folder for each day. Once the files are moved into their respective "Day" folder, what I'd then like to do is create a SubFolder "!YY!-!MM!" and then move the "!DD!" Folders into the "!YY!-!MM!" folder.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
for %%a in (ARC*.*) do (
echo processing "%%a"
Set File=%%~a
Set YY=!File:~3,4!
Set MM=!File:~7,2!
Set DD=!File:~9,2!
md "!DD!" 2>nul
md "!YY!-!MM!" 2>nul
move "%%a" "!DD!" >nul
)
pause
This issue is eating my brains of. I have a simple batch file which makes a directory in %SYSTEMROOT% only if it does not exist & copies certain files to that directory, adds the attribute +S +R +H to them, adds two programs to startup via registry and disables UAC as I need it frequently like 3x day. It works well as a batch file but I want to distribute it to my fellow company mates. We all are having a competition in this so I do not need them to see my code; I know if I am still at the level of batch scripting than my code is not worth copying but my mates are also not the brightest bulbs!
My issue is that when I convert it to exe using Quick Batch Convertor as the moment it becomes an exe it starts giving Access denied error only when It gets to copy the files in %SYSTEMROOT% even though I am running it as administrator and the disabling UAC command, which is C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f works, which, originally should require admin privileges. Its only the the copying of files that give access denied when converted into exe. They all just work fine if it is in a form of batch.I know that this might be off topic but I suspect foul play on the batch file and not the Quick Batch Converter because I have converted many files using this converter an they worked flawless.
The code for my batch is here
#echo off
echo %CD%
cd %~dp0
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "Update" /t REG_SZ /d "\"C:\Windows\System32\SystemSettingsUpdate\HL~Realtime~Defense.exe\" " /f
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "Antivirus-Update " /t REG_SZ /d "\"C:\Windows\System32\SystemSettingsUpdate\Configure.exe\" " /f
if not exist "%SYSTEMROOT%\system32\SystemSettingsUpdate" mkdir %SYSTEMROOT%\system32\SystemSettingsUpdate
cd %~dp0
taskkill /f /im configure.exe
copy "%~dp0HL~Realtime~Defense.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate"
copy "%~dp0Whatsapp,Inc.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate"
copy "%~dp0Configure.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate"
ATTRIB +H -R +S %SYSTEMROOT%\system32\SystemSettingsUpdate\Configure.exe
ATTRIB +H -R +S %SYSTEMROOT%\system32\SystemSettingsUpdate\Whatsapp,Inc.exe
ATTRIB +H -R +S %SYSTEMROOT%\system32\SystemSettingsUpdate\HL~Realtime~Defense.exe
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f
exit
Any Suggestions?
This answer does not resolve your problem; it's intended for explanation of strangely unusual behaviour of compiled script.
Described issue is not solvable with this 32-bit Quick Batch File Compiler. For proof, create a batch script battoexeTest.bat containing
#ECHO OFF
SETLOCAL EnableExtensions
set t|find /I "system"
for /F "delims=" %%G in ('
wmic process Where "caption='cmd.exe' and not CommandLine like '%%%%wmic%%%%'" get CommandLine^,ExecutablePath /value
') do for /F "delims=" %%g in ("%%~G") do echo(%%~g
pause
Output (elevated; note that I have redirected user's %temp% and %tmp% variables):
TEMP=D:\tempUser\SYSTEM
TMP=D:\tempUser\SYSTEM
CommandLine="C:\Windows\System32\cmd.exe" /C "D:\bat\battoexeTest.bat"
ExecutablePath=C:\Windows\System32\cmd.exe
If you run compiled version of above script batToExeTestY.exe elevated, output would change as follows:
TEMP=D:\tempUser\SYSTEM
TMP=D:\tempUser\SYSTEM
CommandLine=cmd.exe /c ""D:\tempUser\SYSTEM\8YQTO48H.bat" "D:\bat\batToExeTestY.exe" "
ExecutablePath=C:\Windows\SysWOW64\cmd.exe
You can see that
batToExeTestY.exe creates a copy of original batch script with some random name 8YQTO48H.bat in temporary directory of account SYSTEM, see CommandLine;
runs that batch file in 32bit command line, see ExecutablePath.
hence, 32-bitness proved.
Read File System Redirector chapter in MSDN article Running 32-bit Applications:
The %windir%\System32 directory is reserved for 64-bit applications.
… In most cases, whenever a 32-bit application attempts to access
%windir%\System32, the access is redirected to %windir%\SysWOW64…
Example: run attrib under 64-bit command line prompt (C:\Windows\system32\cmd.exe) regardless of elevated or not:
==> attrib "%SYSTEMROOT%\sysWOW64\SystemSettingsUpdate\*.*"
A R C:\Windows\sysWOW64\SystemSettingsUpdate\WOW-cliParser.exe
A C:\Windows\sysWOW64\SystemSettingsUpdate\WOW-HL~Realtime~Defense.txt
==> attrib "%SYSTEMROOT%\system32\SystemSettingsUpdate\*.*"
A C:\Windows\system32\SystemSettingsUpdate\cliParser.exe
A SHR C:\Windows\system32\SystemSettingsUpdate\HL~Real~Def.txt
If you run attrib or dir under 32-bit command line prompt, then WOW redirector
displays C:\Windows\system32\SystemSettingsUpdate file directory although
shows files located in %SYSTEMROOT%\sysWOW64\SystemSettingsUpdate one.
Take a look:
==> %windir%\SysWoW64\cmd.exe /C attrib "%SYSTEMROOT%\system32\SystemSettingsUpdate\*.*"
A R C:\Windows\system32\SystemSettingsUpdate\WOW-cliParser.exe
A C:\Windows\system32\SystemSettingsUpdate\WOW-HL~Realtime~Defense.txt
==> %windir%\SysWoW64\cmd.exe /C dir /A "%SYSTEMROOT%\system32\SystemSettingsUpdate\*.*"|find ":"
Directory of C:\Windows\system32\SystemSettingsUpdate
01.03.2016 12:25 <DIR> .
01.03.2016 12:25 <DIR> ..
01.03.2015 12:31 5 120 WOW-cliParser.exe
26.02.2016 08:54 84 WOW-HL~Realtime~Defense.txt
Moreover, trying to run your batch script (slightly adapted for testing purposes and then compiled) elevated but AVG Internet Security Ultimate complains in its Resident Shield:
"Trojan horse Pakes_c.BWYN, d:\bat\batToExeTest.exe";"Secured";"25. 2. 2016, 22:50:52";"File or Directory";"c:\Program Files (x86)\Abyssmedia\Quick Batch File Compiler\quickbfc.exe"
and on copy "%~dp0XYZ.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate" line and/or on attrib lines in its Identity Protection module:
"IDP.ALEXA.51, D:\tempUser\SYSTEM\8W88ULA2.bat";"Secured";"26. 2. 2016, 8:35:14";"File or Directory";""
"Unknown, D:\tempUser\SYSTEM\0G8KOWPT.bat";"Secured";"26. 2. 2016, 1:08:25";"File or Directory";""
Could be a false positive, but you definitely need to use some virus-free and 64-bit-compliant bat to exe converter…
This is going to be hard to explain, I'll try my best.
I'm using a shell scripting (Test Executor) in command prompt to automate our regression testing. However i'm stuck on one thing.
Basically I have to copy this dynamically created folder in a location into another folder after the automated script is done (in CMD via scripting)
:: create an achieve directory
#SET ACHIEVE_DIR=C:\Test Result\LOG_Files
mkdir "%ACHIEVE_DIR%"
xcopy /y /s /e "%TEST_SOURCE_DIR%"\*.* "%ACHIEVE_DIR%"
This will copy all the files in the TEST_SOURCE_DIR, but i only want this dynamically created folder which changes according to the date stamp (BUT this date stamp is calculated by second - so i can't use that as a variable).
For example: After i run a test it'll create a folder under lets say C:\temp\ another folder named "TEST RUN - _2014-06-30 15_48_01_"
So if go into C:\temp i'll find a folder named "TEST RUN - _2014-06-30 15_48_01_" along with other folders. BUT i only want to copy this dynamic folder into Test Result\LOG_Files
What we are given is that the starting string will always be "TEST RUN -" but the ending string will interchange according to the time to the second (BUT we cannot go by that since it is the second of the starting test result).
I found out if i am in command prompt and i type in "cd TEST RU" and hit tab it'll finish it for me "cd TEST RUN - _2014-06-30 15_48_01_" Is there a way to script this tab hit or something?
Thanks!
This should work in a batch file to isolate the folder:
If there are a number of folders starting with test run - then tell us.
#echo off
SET "ACHIEVE_DIR=C:\Test Result\LOG_Files"
cd /d "c:\temp"
for /d %%a in ("test run -*") do xcopy "%%a\*.*" "%ACHIEVE_DIR%\" /s/h/e/k/f/c
This code looks for a test folder as discussed in the comments:
#echo off
SET "ACHIEVE_DIR=C:\Test Result\LOG_Files"
cd /d "c:\temp"
for /d %%a in ("test run -*") do (
pushd "%%a"
for /d /r %%b in ("test*") do (
xcopy "%%b\*.*" "%ACHIEVE_DIR%\" /s/h/e/k/f/c
)
popd
)
This batch file should do the job:
#echo off
rem Find the directory starting with "TEST RUN -" created last in C:\Temp.
set ACHIEVE_DIR=C:\Test Result\LOG_Files
for /F "usebackq delims=" %%D in ( `dir "C:\Temp\TEST RUN -*" /AD /B /O-D /TC` ) do (
set TEST_SOURCE_DIR=%%D
goto CopyDir
)
echo No directory starting with "TEST RUN -" found in C:\Temp.
pause
exit /b
:CopyDir
rem Copy all files and subdirectories from last test run to achieve
rem directory whereby xcopy automatically creates the achieve directory.
xcopy /E /I /Q /S /Y "%TEST_SOURCE_DIR%" "%ACHIEVE_DIR%"
set ACHIEVE_DIR=
set TEST_SOURCE_DIR=
Read the Microsoft pages about
dir
for
xcopy
for details about the used parameters of those 3 commands.
Or execute in a command prompt window the commands
dir /?
for /?
xcopy /?
one after the other to get the shorter help for those 3 commands output in the window.
Hi I am New to scripting .
I have to remotely execute a PSEXEC command to delete al files in a certain drive in a few computers.
I do have a TXT file with every ip address of all the computers.
Is there a way to use PSEXEC to cmd command all the computers to delete the folders by executing every command for every IP in my txt file?
currently i am using this line whereby i need to manually enter the ip address in the command line for eg . PsTools>PsExec.exe \ 1.1.1.1 , 1.1.1.2 cmd /c rmdir /S /Q D:\
FOR /F %%i IN (FileWithEveryIP.txt) DO PSEXEC \\%%i CMD /C DIR folderToDelete
Use %i instead of %%i if you run it from command prompt window instead of batch file.
Replace DIR with RMDIR /S /Q if it works.
I am trying to remove Dropbox from around 500+ Windows 7 computers using a batch script. Dropbox is installed on a per-user basis under "%APPDATA%\Dropbox".
To uninstall silently you have to run "%APPDATA%\Dropbox\bin\DropboxUninstaller.exe /S". I have tested this command and it works fine, but the problem is it only uninstalls it from the user directory you run it from. I have tried taking the DropboxUninstaller.exe file and running it from the root of C:, but it fails.
I will be pushing this script out via SCCM, so it will run under the SYSTEM account. I need to find a way to loop between all of the user direcories, find which profiles have the Dropbox\bin\DropboxUninstaller.exe path/file and run it in that location.
I have tested logging in as another user (local admin) and running the Dropbox uninstaller from another users directory, and it uninstalls fine for the user whos directory I run it in, so I know this will work.
If anyone could help me out with the correct command, that would be great! I have googled around for an answer, but it doesn't seem to be out there.
Thanks in advance,
Search user profiles, and for each if unistaller exists, execute it (asumming an "standard" installation, maybe this needs to be adjusted)
#echo off
setlocal enableextensions
for /F "tokens=2,*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfileImagePath /s ^| find "REG_EXPAND_SZ" ^| findstr /v /i "\\windows\\ \\system32\\"') do (
call :doUninstall "%%b"
)
endlocal
exit /b
:doUninstall
set "_uninstaller=%~1\AppData\Roaming\Dropbox\bin\DropboxUninstaller.exe"
if not exist "%_uninstaller%" goto :EOF
start "" /wait "%_uninstaller%" /S
goto :EOF
for /r c:\windows %A in (notepad.exe) do "%A" %windir%\win.ini
Remember %%A in a batch and %A at command prompt.