Thursday, June 16, 2011

Robocopy error log

C:\>type robocopy.log | find "(0x" > robocopyerrors.log

If you've used robocopy to back up files and log the process, you probably have a massive, unwieldy robocopy log which lists every single file that was copied. That's all fine, but what if it tells you there were 100 files that failed to copy? How do you know which ones?

Now, I'll admit, if you only have a small handful of failed files, it's probably easier to open the log in notepad and find them with CTRL-F. You can search for "error," but that may not be ideal if you have a lot of files with "error" in the path. A better search term is "(0x", which is the beginning of the error code number.

Once the number of errors hits double-digits, it's probably better to be able to see the errors by themselves without dealing with having to click Find Next. The above command will list the entire file, find only the lines containing error codes, and save the results as robocopyerrors.log. The only real downside is that you don't get the human-readable error, just the code. The error message is saved one line below the error code, so retrieving it would involve some complex command line jujitsu. I may tackle that some other day.

Wednesday, June 8, 2011

Robocopy backups

C:\>robocopy c:\users\joe\documents e:\backup /copyall /mir /r:0 /w:0 /log:c:\users\joe\desktop\robocopy.log

I wasn't sure about including robocopy here, because it's not built in to XP, but it comes with Windows 7 (and I think Vista too). You can download it for XP from the Windows Server 2003 Resource kit here. This particular command will back up Joe's documents folder to a folder called "backup" on the E: drive, presumably a USB drive.

It will (/copyall) copy all NTFS rights (irrelevant on a FAT32-formatted flash drive), it will (/mir) mirror content, meaning it will only back up changed files or files with newer time stamps, but it will also delete* any files not existing on the source. It will not (/r:0 /w:0) retry failures, and it will create a log of everything on the desktop.

*Be VERY careful with the /mir switch. The FIRST thing it does is delete folders that don't exist on the source. So if you accidentally point it at the root of your external drive instead of the backup folder, it will wipe it out before you have a chance to stop it.

Thursday, June 2, 2011

Take admin ownership

C:\>takeown /f . /a /r

This will assign ownership of the current directory, and all files and folders below it, to the local admins group (BUILTIN\Administrators). Ten minutes ago, I didn't know the takeown command existed! (It's new to Vista/Server 2008)

The /f switch specifies which file(s) (single dot means current dir), the /a assigns ownership to the admins group instead of specifying a specific user, and the /r recurses through all files/folders.

Wednesday, June 1, 2011

Domain admins full access

C:\>icacls * /grant "Domain Admins":(OI)(CI)F /c /t

I don't know if it's just me, but my NTFS rights tend to get screwed up a lot. Sometimes the Domain Admins group loses rights to files, among other issues. Previously, I would use:

C:\>cacls * /e /g "Domain Admins":F /c /t

to accomplish this, but I started running into problems with the 255-character limit on path names. (Why people think they have to nest their folders so deep is beyond me.)

Tuesday, May 31, 2011

Clear the print spooler

C:\>net stop spooler && del /q %WINDIR%\system32\spool\printers\* && net start spooler

Stops the print spooler service, clears out all queued print jobs, and restarts the spooler. This will fix a LOT of printer issues.

Copy matching files

C:\>cmd /v:on /c "for /f %i in ('dir /b *.JPG') do @(@set a=%i && @copy ..\!a:JPG=DNG! && @del /q !a!)"

I don't really use a photo management program. I know I should, but all the ones I've tried were either crap or expensive. When organizing photos from a particular event, I have a folder, say "Zoo," that contains the full-resolution raw files and the 2MP JPEG files from my camera. (I set my camera to RAW+JPEG, but the JPEG size to 2MP for easier management.) As I go through the images, I copy the good ones into a folder called "keepers." Afterward, I want to take the low-res JPEGs and replace them with the full-res raw files for processing.

At it's heart, this command is just:

C:\>for /f %a% in ('dir /b *.JPG') do @(@copy ..\%a:JPG=DNG% && @del /q %a%)

In other words, for all of the .JPG files in this directory, copy the corresponding .DNG file from the parent directory. If (and only if) that worked, delete the JPG.

But, as always, Windows needs a little handholding, first variable operations on a temporary loop variable, and then, to enable delayed expansion so that variables work in FOR loops.

In Linux, which is generally where I need this operation, it's a LOT more elegant:

for i in *;do cp ../${i%JPG}DNG . && rm $i;done

Saturday, May 28, 2011

Remove hidden attribute

C:\>attrib -h c:\* /s

I've seen a couple of malware infections lately that have set all files to hidden. Needed to run this to reset the attributes.

Install a network printer

C:\>rundll32 printui.dll,PrintUIEntry /in /n\\SERVER\hplaserj

Quick and easy. If the printer is shared correctly on the server with the right drivers for the client architecture and whatnot, nothing else is needed.

Friday, May 27, 2011

Open firewall ports

C:\>netsh firewall set portopening tcp 23 telnet

I don't think much explanation is required for this one...

Change IP address

C:\>netsh int ip set addr local dhcp
C:\>netsh int ip set addr NIC1 static 192.168.1.10 255.255.255.0 192.168.1.1 1
C:\>netsh int ip set addr "Local Area Connection 5" dhcp

The first one works if the connection you want to configure is "Local Area Connection," and they may also work if you have "Local Area Connection 2," but no others. If you're going to use this command on your own PCs with multiple NICs, it'd be better to rename your connections to "NIC1," "NIC2," etc, in which case you can use the second example. If you have multiple "Local Area Connections," you'll have to use the third one with the quotes.

Also note with the static address definition, it should be obvious that the format is IP[space]netmask[space]gateway, but you may not have noticed the [space]gateway metric at the end. To be perfectly honest, I don't even know what this means; just put a 1.

Grant local admin rights

C:\>runas /env /user:administrator "cmd /t:2f /k net localgroup Administrators /add %USERDOMAIN%\%USERNAME% && pause"

You'll need the local admin password, obviously, or use /user:DOMAIN\administrator or another admin user.

The /t:2f (optional) sets the background to green so you know it's an elevated prompt. The /env stays in the current user's environment, otherwise the %USERNAME% variable would expand to "Administrator"

Also, your current directory must be on a local drive accessible to Admin, or else you'll get a "Directory name is invalid" error.

Original OS install date

C:\>systeminfo | find "Install Date"

This one's not terribly useful, as most machines have been imaged, and this will keep the image's date. But it can still be good to know if an install is acting wonky.

System Uptime

C:\>systeminfo | find "System Up Time"

Will tell you how long since a computer's been rebooted. I use this when someone has a problem and SWEARS that they rebooted their PC four times this morning.

"Umm... well it says here you haven't rebooted since March. You know sleep and hibernate don't count, right?"

Notes:
Hibernate is an interesting beast, because it allows you to physically reboot the hardware without rebooting the software. I once hibernated a PC and replaced the hard drive. Several months later, I reinstalled the original drive to check something, and systeminfo reported the uptime as months.

Also, as always, FIND is case sensitive.

Ping a range of IPs

C:\>for /l %i in (1,1,254) do @ping 192.168.1.%i -n 1 -w 100 | find "Reply"

This will ping all addresses from 192.168.1.1 to 192.168.1.254 one time each, wait 100ms for a reply (more than enough time on a local network) and show only the addresses that replied.

Syntax for FOR /L is (start,step,end) if you want to change the range.

Also, note that the Windows FIND is case sensitive, so make sure you capitalize "Reply," or else use the /i switch or just "eply."