Scratch your snatch, and share a batch
Page 1 of 1
scaramonga




Posts: 9800

PostPosted: Wed, 22nd Apr 2020 03:30    Post subject: Scratch your snatch, and share a batch
With us all being utterly bored during these bleak times, I've taken to creating batch files for simple tasks, thus relieving my desire to hang my neck, but seeing as I no longer can shop for 'rope', I thought it would be good to share and compare Smile

Through these hard times (not CV-19 related), but regarding OS shit, Wink one must test 'other' possibilities available to them, and it can become tiring having to reset that same old flash drive again, and again.

Fear not.

This will clean up your little drive in seconds, all ready for that next install of Linux (LOL). Just think of it as a 'wipe & go' method Smile

One of the problems of using Diskpart, is that everyone's drives and volumes change, and having to go in and 'list disk' is tedious, and getting it wrong can be dangerous, well, no longer.

This batch will select correct drive based on your volume, and if your using same flash drive all the time (as is myself), then it does save a whole lot of tedium.

I've highlighted what you need to change, most importantly, the volume figure. I'm using a 32Gb flash drive, so 29 is my setting, thus Diskpart will only select that drive each time it's run.

THE BATCH MUST BE RUN AS ADMIN!! (Yes, I could implement that in script, but that is what your here for, to improve! )

The other settings are as you desire, for instance, I have NTFS as format option, so that can be changed to whatever you wish. Just add them in as required.

Code is below, just save as a .bat file.

Code:
REM -scaramonga- 4/22/2020
REM
@ECHO OFF
@CLS
REM
REM Get volume list and export to file
ECHO LIST VOLUME>>C:\VOLLIST.TXT
REM
REM Execute text file as script with Diskpart then export results to new text file
DISKPART /S C:\VOLLIST.TXT>>C:\VOLTEMP.TXT
REM
REM Delete VOLLIST.TXT as we no longer need
DEL C:\VOLLIST.TXT
REM
REM Find volume from list and select correct drive, adding this to VOLTEMP.TXT
FOR /F "TOKENS=2 DELIMS= " %%A IN ('TYPE C:\VOLTEMP.TXT ^| FIND /I "29 GB"') DO ECHO SELECT VOLUME %%A>>C:\VOLACT.TXT
REM
REM Delete VOLTEMP.TXT as we no longer need
DEL C:\VOLTEMP.TXT
REM
REM Append additional Diskpart commands to VOLACT.TXT
ECHO clean>>C:\VOLACT.TXT
REM
ECHO create partition primary>>C:\VOLACT.TXT
REM
ECHO format quick fs=ntfs>>C:\VOLACT.TXT
REM
REM Execute VOLACT.TXT through Diskpart as script
REM
DISKPART /S C:\VOLACT.TXT
REM
REM Delete VOLACT.TXT as we no longer need
REM
DEL C:\VOLACT.TXT
REM
REM Pause to see if there are any errors?
@PAUSE
Back to top
Newty182




Posts: 10797
Location: UK
PostPosted: Wed, 10th Jun 2020 20:25    Post subject:
Cheers. Just sent this out with Landesk to the 3000 computers across the estate where I work. What does it do?


Ryzen 5 5600, ASUS ROG STRIX B550-F GAMING WIFI II, Corsair Vengeance RGB RT 32GB 3600MHz C16, Zotac RTX 3060 Ti, Corsair RMx Series RM750x. AOC AGON AG324UX - 4K 144Hz 1ms GTG IPS FreeSync KVM
Back to top
scaramonga




Posts: 9800

PostPosted: Wed, 10th Jun 2020 23:42    Post subject:
Newty182 wrote:
Cheers. Just sent this out with Landesk to the 3000 computers across the estate where I work. What does it do?


Finds and selects a volume based on its size, then formats said drive with your chosen file system via Diskpart, all in one click Wink

I use this daily, on a 32Gb thumb drive, as I test out various Linux distros, and it's tedious having to manually wipe and format beforehand, which is what you are supposed to do Smile

Nothing fancy happening here, lol. It will use 'C:' drive to create 'temp' .txt files to do its job, and detect your drive based on volume size parameter. File system parameter can also be changed to whatever, e.g FAT32, NTFS etc. too. It will fall down if you have more than one disk size the same, so best to just use for the odd USB drive that is plugged in.

You could also add 'convert gpt' or 'convert mbr' to the script as well, if you are changing partition systems on a whim, keep one for mbr, and one for gpt, yada yada Smile

Makes my life easier lol, plus, as I said, I was bored Laughing
Back to top
Ampee




Posts: 1986

PostPosted: Thu, 11th Jun 2020 10:32    Post subject:
Nice to see someone using batch.

And an advice:
Start using PowerShell. It can do the same stuff as batch and more.
Like your script probably can be done in one line using PowerShell.
Also: no need to REM each line.
Back to top
scaramonga




Posts: 9800

PostPosted: Sun, 14th Jun 2020 02:05    Post subject:
Ampee wrote:
Nice to see someone using batch.

And an advice:
Start using PowerShell. It can do the same stuff as batch and more.
Like your script probably can be done in one line using PowerShell.
Also: no need to REM each line.


Cheers buddy! Smile

Thx for the tips, much appreciated, and we are all here to improve on it. I really must get into PowerShell though.

Back to top
Newty182




Posts: 10797
Location: UK
PostPosted: Sun, 14th Jun 2020 15:27    Post subject:
Ampee wrote:
Nice to see someone using batch.

And an advice:
Start using PowerShell. It can do the same stuff as batch and more.
Like your script probably can be done in one line using PowerShell.
Also: no need to REM each line.


Powershell is what I'm starting to learn. Everyone at work pretty much uses batch files and although it gets the job done, most of the time, powershell would be the best way for a lot of things

Edit: For example. I needed to deploy a batch file to run as the admin account, to install software and then create an ODBC connection with reg entries as the current user. I couldn't see a way to do this in Landesk with just one task and so I had to create two tasks and bundle them together. I'm sure I could have done this easily as one script with powershell


Ryzen 5 5600, ASUS ROG STRIX B550-F GAMING WIFI II, Corsair Vengeance RGB RT 32GB 3600MHz C16, Zotac RTX 3060 Ti, Corsair RMx Series RM750x. AOC AGON AG324UX - 4K 144Hz 1ms GTG IPS FreeSync KVM
Back to top
Ampee




Posts: 1986

PostPosted: Sun, 14th Jun 2020 15:43    Post subject:
Lemme know if you need PowerShell help/tutorial.

ODBC type of connection can be done in PowerShell using .net calls (System.data.odbc if i remember correctly)

current user can be found out using quser (which requires a bit of manipulation, and since it's an external binary you would need to treat exceptions using lastexitcode) and then relying on WMI for the SID, and then mounting the hive as a PSDrive.

Registry manipulation can be done with built in cmdlet (Set/Get-Itempropety) relying on the PSDrive offerings. (Or of course .net)
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Operating Systems
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group