Skip to content

Instantly share code, notes, and snippets.

@ChadDevOps
Last active August 24, 2021 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChadDevOps/3102c934a58ac0b6476ea34b15f01331 to your computer and use it in GitHub Desktop.
Save ChadDevOps/3102c934a58ac0b6476ea34b15f01331 to your computer and use it in GitHub Desktop.
Generates a random shortname for directories, unzips files, generates png from stl, generates png from svg
# Run as admin
# Summary: Generates a random shortname for directories, unzips files, generates png from stl, generates png from svg
# Pre-req:
# Install OpenSCAD (Ex using chocolatey: choco install openscad -y)
# Install inkscape (ex using choco: choco install inkscape -y)
# Install superdelete (ex using choco: choco install superdelete -y)
# Enable Shortnames in Windows 10 by running the following commands (helps with 260 char. limits):
# fsutil 8dot3name query
# fsutil behavior set disable8dot3 0
# Post [optional]
# If you want to convert png to jpg run the following from wsl (you'll need imagemagick and parallel)
# find . -iname "*.png" | parallel convert -quality 95% {} {.}.jpg
# find . -iname "*.png" | parallel rm {}
# Change all dates - windows (useful to get the photos app to show all images under one date, eg. 2099-02-02):
# https://github.com/nrogoff/ImageProcessingConsole
# Change all dates - linux/wsl:
# sudo apt-get install libimage-exiftool-perl
# find . -iname "*.jpg" | parallel "exiftool -overwrite_original -AllDates='2099:02:02 02:02:02' -datetime='2099:02:02 02:02:02' {} && touch -a -m -t 209902020202 {}"
# Copy all jpgs and file structure - linux/wsl:
# find . -name '*.jpg' | parallel "cp -u --parents {} ../all_images"
Get-ChildItem -Recurse -Directory | ForEach-Object {
# or in reverse comment out above and uncomment below
# Get-ChildItem -Recurse -Directory | sort @{expression = {$_.fullname}} -DESC | ForEach-Object {
$fullpath = $_.FullName
echo $fullpath
$foldername = $_.Name
$ShortPath = (New-Object -ComObject Scripting.FileSystemObject).GetFolder($fullpath).ShortPath
$ShortName = $ShortPath|split-path -leaf
if($ShortName.Contains('~')) {
#echo "'$foldername' is MSDOS: $ShortName | $fullpath"
}else{
if ($ShortName.length -gt 8) {
if($foldername -match '^[a-zA-Z0-9]'){
[string]$first = $foldername.SubString(0,1)
}else{
[string]$first = Get-Random -Minimum 1 -Maximum 9
}
[string]$num = Get-Random -Minimum 1000 -Maximum 9999
[string]$newShortName = $first + $num + '~1'
echo "creating MSDOS $newShortName for '$foldername' at $ShortPath"
& fsutil.exe file setshortname "$ShortPath" $newShortName
}else{
# do nothing or set shortname to nothing by uncommenting the below
& fsutil.exe file setshortname "$ShortPath" """"
}
}
$ShortPath = (New-Object -ComObject Scripting.FileSystemObject).GetFolder($fullpath).ShortPath
# ZIP STUFF
Get-ChildItem -Path "$ShortPath/*" -File -Include *.zip, *.rar | ForEach-Object {
#if(Test-Path -Path $_.Name -PathType Leaf){
write-host '-+-+-+-+-+-+ EXTRACTING'
$fullorigpath = $_.FullName
$filenameandext = Split-Path $_.Name -leaf
$parent="$(Split-Path $_.FullName -Parent)";
$parent = (New-Object -ComObject Scripting.FileSystemObject).GetFolder($parent).ShortPath
$origPath = $parent + '\' + $filenameandext
$filename = [io.path]::GetFileNameWithoutExtension($_.FullName)
$outpath = $parent + '\' + $filename
$temp = New-Item -ItemType Directory -Force -Path $outpath
$OutShortPath = (New-Object -ComObject Scripting.FileSystemObject).GetFolder($outpath).ShortPath
write-host "From: $origPath"
write-host "To: $OutShortPath"
$arguments=@("-aoa", "x", "`"$origPath`"", "-o`"$OutShortPath`"");
$ex = start-process -FilePath "`"C:\Program Files\7-Zip\7z.exe`"" -ArgumentList $arguments -wait -PassThru -NoNewWindow;
if( $ex.ExitCode -eq 0)
{
write-host "Extraction successful, deleting $origPath"
#rmdir -Path $origPath -Force
superdelete --silentMode "$fullorigpath"
}
write-host
write-host
#}
}
# SVG STUFF
Get-ChildItem -Path "$ShortPath/*.svg" -File | ForEach-Object {
[string]$svgname = $_.Name
[string]$svgpath = $ShortPath + '\' + $svgname
[string]$name = [io.path]::GetFileNameWithoutExtension("$svgpath")
[string]$pngname = $name + '.png'
[string]$file = $ShortPath + '\' + $pngname
if (-not(Test-Path -LiteralPath "$file" -PathType Leaf)) {
inkscape --export-type="png" "$svgpath"
echo "creating '$pngname' for '$ShortPath\$svgname'"
}
}
# STL STUFF
Get-ChildItem -Path "$ShortPath/*.stl" -File | ForEach-Object {
[string]$stlname = $_.Name
[string]$stlpath = $ShortPath + '\' + $stlname
[string]$name = [io.path]::GetFileNameWithoutExtension("$stlpath")
[string]$pngname = $name + '.png'
[string]$jpgname = $name + '.jpg'
[string]$file = $ShortPath + '\' + $pngname
[string]$file2 = $ShortPath + '\' + $jpgname
if (-not(Test-Path -LiteralPath "$file" -PathType Leaf)) {
if (-not(Test-Path -LiteralPath "$file2" -PathType Leaf)) {
[string]$tmpname = "__tmpSTL4L1F3__" + $name + "__tmp"
[string]$tmppath = $ShortPath + '\' + $tmpname
cmd.exe /c "echo import(""$stlname""); >""$tmppath"""
cmd.exe /c "c:/PROGRA~1/OpenSCAD/openscad.exe -o ""$file"" --autocenter --viewall --imgsize=1080,1080 ""$tmppath"" > nul 2>&1"
echo "creating '$pngname' for '$ShortPath\$stlname'"
}
}
}
# Delete stuff
# $ShortPath = (New-Object -ComObject Scripting.FileSystemObject).GetFolder($ShortPath).ShortPath
#if((Get-ChildItem "$ShortPath" | where-object {$_.FullName -match ".*__tmpSTL4L1F3.*"}).Count -gt 0){
Get-ChildItem -Path "$ShortPath/__tmpSTL4L1F3*" -File | ForEach-Object {
#write-host "Deleteing $ShortPath\__tmpSTL4L1F3*"
$fullpathtofile = $_.FullName
superdelete --silentMode "$fullpathtofile"
}
# }
}
@ChadDevOps
Copy link
Author

I use this primarily for extracting the STL zips from thingiverse and converting them to png. Also for cricut svg files to png for viewing in photos or faststone image viewer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment