I'm currently working in an organization that has migrated their OU groups so that everything belongs to a parent folder called "users and workstations".
This is a slight problem for me, as I have a few batch scripts that delete users and computers from a text file - something that I run fairly regularly.
The current code I use, for example, to disable a batch of machines is below
#echo off
CLS
ECHO Now Disabling Machines...
TIMEOUT 2 > nul
Pause
FOR /f %%i in (%~dp0\computernames.txt) do (
dsquery computer -name %%i | dsmod computer -disabled Yes
)
If I run this code since the change, I get the following error
dsmod failed:'Target object for this command' is missing.
type dsmod /? for help.
However, if I manually type the dsquery / dsmod line of code into command prompt and replace the "%%i" with a computer that failed, it succeeds.
I'm almost certain that this is due to the spaces within the OU folder structures, but don't know what to do to change my script to continue working.
Is there a way to change it? should I try something else? I'm going crazy trying to figure this out!!!
Thanks in advance,
Ben
P.S. I've come up with a solution that seems to work - I'll keep this open incase anyone can suggest a better way to do what i need to do. Please see below for the code that works for me. Looks like I needed to add correct delims and two sets of double-quotes to exit out correctly.... it doesn't make too much sense to me... but it works...
#echo off
setlocal disabledelayedexpansion
CLS
ECHO Now Disabling Machines...
ECHO.
Pause
FOR /f "delims= " %%i in (%~dp0\computernames.txt) do (
echo disabling %%i && echo. && dsquery computer -name ""%%i"" | dsmod computer -disabled Yes && echo.
)
Try using quotes on %%i in this line like this
dsquery computer -name "%%i" | dsmod computer -disabled Yes
Edit (added below):
Ahhhh... I bet the current directory is not what you think. Perhaps you are executing from a UNC drive. Try this:
pushd %~dp0
FOR /f %%i in (computernames.txt) do (
dsquery computer -name "%%i" | dsmod computer -disabled Yes
)
Please see original post for solution.
disabling delayed expansion and specifying the delims, along with double double quoting the variable seems to have fixed the problem!
Related
I am attempting to hash all or most of the files off of a machine using a batch script. What I thought would be straight forward was of course not as FCIV will not scan hidden files. I attempted to make a for loop that would scan the individual files themselves but what works in the command line does not work in the batch file.
I would go to the root of my drive and attempt this:
FCIV -r -both c:\
However I noticed that quite a few files were missing (even as admin) with most of them being hidden files.
Thanks,
Any help would be appreciated.
I use this snippet of the code for CertUtil, it might also work for FCIV
cd /d %targetDir%
for /r %%e in (*) do (
if exist "\\?\%%e" (
FOR /F "tokens=* USEBACKQ" %%F IN (`certutil -hashfile "\\?\%%e" %hashType% ^| findstr /v "certutil hash"`) DO (SET var=%%F)
echo !var! "%%e" >> %hashDatabaseOutput%
SET /A fileCount += 1
) else (
echo ERROR 1 - Failed to Hash file, File or Directory contains special characters >> %errorlog%
echo %%e >> %errorlog%
echo.>> %errorlog%
)
)
cd /d "%workingDir%"
I have a similar issue as you, I'm scanning some files in C:\ but those aren't hidden files.
EDIT: Seems that using this method makes no difference because even when the file path that is feed to FCIV, the hashing will fail because it cannot find the file. you can look at my question here https://stackoverflow.com/questions/62111957/fciv-fails-to-find-some-files-when-hashing-c-drive
EDIT2: Found the root cause, it's caused by redirection when you attempting to scan files in C:\Windows\System32. One guy explained it clearly here
The system cannot find the path specified
Since FCIV only have 32bit mode, the only solution is to use other hashing program like rHash with 64bit.
For my computer project, my teacher requires us to make a bat file that lists down hidden files. The whole class doesn't know how though. Here is my code so far:
#echo off
set /p UserInput=What directory would you like?
cd %UserInput%
dir /S /aH
Pause
My teacher says I'm almost done, I'm just missing 2 or 3 characters
after "dir /S /aH". Would anyone know what these missing characters are?
Hi guys, I'm sorry for the late reply. We're having so many projects before the semester ends. :( When I run the bat file, it shows the following.. Here's the link: imgur.com/W4Wm4vP
Whilst you've already received responses that are sufficient to your initial request, I've added one just to show that you should really try to perform some sort of verification of user input before proceeding with your listing:
#ECHO OFF
:LOOP
CLS
SET /P "UserInput=What directory would you like? "
IF "%UserInput%"=="" GOTO LOOP
IF /I NOT "%CD%"=="%UserInput%" (PUSHD "%UserInput%" 2>NUL||GOTO LOOP)
DIR /B /S /AH-D
TIMEOUT /T -1 /NOBREAK
I trust this will give you a few more things to research and learn.
Edit
As an after thought, if the end user is inputting only a directory name and not a full path then you may need to replace line six with:
FOR %%A IN ("%CD%") DO IF /I NOT "%%~nxA"=="%UserInput%" (
PUSHD "%UserInput%" 2>NUL||GOTO LOOP)
I use the following batch file to delete unwanted files on several drives.
set /p DELPATH=<"C:\DELETE-ALL.txt"
for /f "usebackq delims=;" %%i in ("C:\DELETE-ALL.txt") do #del /q "D:\HFI\%DELPATH%\%%i" > C:\DELETE-ALL-4.txt 2>&1
... same command for other local and network drives.
The DELETE-ALL.txt looks like this:
mydirectory
TEST.xlsx
TEST2.xlsx
This works great. It deletes files in single directory. But now I need it to do more. I need the batch file to delete files in different directories. So, it boils down to how to change directory on the fly.
Any help will be greatly appreciated.
I answer you here because i can't comment now with my lower reputation.
I strongely recommend to use PowerShell or python or others program scripts to do this. Using windows batch, it will take you more time to find a good way and there may be no way to do such a little complex misson.
The answer turns out to be easier than I thought. Although my original question was for deleting files, I got it to work for rename. It should work for delete with little modification.
#(for /f "tokens=1,2 delims=;" %%A in ('"TYPE C:\RENAME-ALL.txt"') do (
#echo "%%A" | find /i "\"
#if errorlevel 1 (
RENAME "%%A" "%%B" >> C:\RENAME-ALL-4.txt 2>&1
) ELSE (
CD /D D:\mydirectory\%%A
)
)
)
The script looks for "\". If found, it assumes that line is a directory and change to the corresponding directory with "D:\mydirectory\" as a path prefix. Otherwise, it assumes the line contains file name. Since back slash is not allowed in filename, the assumption is safe.
Hope this will help other people.
I did do research but I could not figure out, sorry!
Google can only help me to add characters but not delete characters...
say under the dir E:\Movies\2011\
there are several folders
e.g.
[0603] Movie_1
[0708] Movie_2
[0811] Movie_3
and so on..
So I want to run a batch script to remove date tag
i.e.
Movie_1
Movie_2
Movie_3
and so on
I am using windows 8.
RENAMER.CMD
#echo off
setlocal enableextensions enabledelayedexpansion
for %%i in (*) do (set name=%%i && ren "!name!" "!name:~7!")
endlocal
Explanation:
The setlocal / endlocal stuff just makes sure that the script will work no matter what your default commandline settings are. Those lines are optional if you make sure that command extensions and delayed expansion are enabled by default, or if you start your command prompt using cmd /e:on /v:on.
The for statement is the heart of the thing. It selects every file in the directory using (*). If you only want to change files with certain extensions you can use a different pattern such as (*.avi). Then !name:~7! takes seven characters off the front. set /? lists some of the other manipulations you can perform. Most of the examples use % instead of ! but all string manipulations work with both.
Updated to answer comment
You can perform a % substitution inside a ! substitution.
#echo off
setlocal enableextensions enabledelayedexpansion
set numtrim=7
for %%i in (*) do (set name=%%i && ren "!name!" "!name:~%numtrim%!")
endlocal
You can use the enhanced variable substitution features to do this (SET /? for information):
#echo off
setlocal
for /d %%i in (*) do call :rename %%i
goto :eof
:rename
set CURDIR=%1
echo ren "%CURDIR%" "%CURDIR:~7%"
The magic happens at %CURDIR:~7%, which means "the value of %CURDIR%, but skip the first 7 characters".
The batch file above does not rename anything (it just prints the rename command), so you can do a dry run first and then remove the final echo to get the real job done.
Since you are using Win8, you can use powershell to also accomplish this. Here is what I used to turn
blah11-1
blah12-1
blah13-1
to
blah11
blah12
blah13
C:\testing> dir -Directory | foreach { if($.Name.Length -gt 6){ $fileName = $.Name.SubString(0,6)} else { $fileName = $.Name }; rename-item -path $.fullname -newname $fileName; }
Now this is just what I ran on the powershell command line, but it could be moved into a script to make it more reusable.
Currently I have a set of software-Installations(and their paths) that i have to install on my Windows Machine.
What I do now is -- Hit RUN every time and type in the software installation path into it..
What I want is to design a Batch file which would install all the applications and REBOOT my system after every successful installation and then continue with the NEXT item in the list..
Is it possible using a .bat file ??
This really isn't something batch was designed for, so this will be a bit hacky. It's not elegant by any means but give it a shot, it might work for you.
for /f %%a in (C:\files.txt) do (
start /wait %%a
exit /b
)
for /f "skip=1" %%b in ("C:\files.txt) do (
echo %%b >>C:\newfiles.txt
)
xcopy C:\newfiles.txt C:\files.txt /y
del C:\newfiles.txt /f /q
shutdown /r /t 0 /f
The idea being that you have a text file with the paths of the executables that you want to install. It will go through and execute the first file in the list, wait for it to complete, then re-write the list without the file it just installed.
This is dependend on the setup file having no user interaction and exiting by itself, or maybe it's just to make things easier - in which case just go through each install yourself, and when it finishes the batch file will do the rest.
On the note of rebooting and continuing you will either need to run the batch file again yourself or put it in the registry to start up itself, the latter command being
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v "MyBatchInstaller" /d "C:\MyBatchFile.bat" /f
Hope this helps