Pulling images from Windows 10 Lock Screen to a different folder Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Fastest Windows script to move files from one did to a network dirUpload multiple files in different formats and sizes from one HTML formMoving repeated files from one folder to another

Generate an RGB colour grid

How were pictures turned from film to a big picture in a picture frame before digital scanning?

How to compare two different files line by line in unix?

What is the meaning of 'breadth' in breadth first search?

One-one communication

How to dry out epoxy resin faster than usual?

Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?

How does the math work when buying airline miles?

Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?

How do living politicians protect their readily obtainable signatures from misuse?

How to write capital alpha?

Most bit efficient text communication method?

What does it mean that physics no longer uses mechanical models to describe phenomena?

Would it be easier to apply for a UK visa if there is a host family to sponsor for you in going there?

Do wooden building fires get hotter than 600°C?

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

Lagrange four-squares theorem --- deterministic complexity

How to run automated tests after each commit?

Amount of permutations on an NxNxN Rubik's Cube

How could we fake a moon landing now?

Sentence order: Where to put もう

How to play a character with a disability or mental disorder without being offensive?

What is "gratricide"?

Can you explain what "processes and tools" means in the first Agile principle?



Pulling images from Windows 10 Lock Screen to a different folder



Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Fastest Windows script to move files from one did to a network dirUpload multiple files in different formats and sizes from one HTML formMoving repeated files from one folder to another



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0












$begingroup$


Windows 10 has an interesting feature where it will display "fun facts" and images on the lock screen. Occasionally, these images are something I would want to use for a background.



These images are stored in %LocalAppData%PackagesMicrosoft.Windows.ContentDeliveryManager_cw5n1h2txyewyLocalStateAssets.



As I wasn't certain if _cw5n1h2txyewy would be constant on the Microsoft.Windows.ContentDeliveryManager folder, I wanted to just search the %LocalAppData%Packages directory for Microsoft.Windows.ContentDeliveryManager*, and pull the first directory that matches that pattern.



Then, in that directory, I wanted to copy the images and read a "magic number" from each image, which can vary but determines the image type. In this case, I use the first 4 bytes as 0x89 0x50 0x4E 0x47 (or 0x89 then PNG), or bytes 6 through 9 as 0x4A 0x46 0x49 0x46 (JFIF) or 0x45 0x78 0x69 0x66 (Exif). The first condition would add the png extension, the second would add jpg.



The first function is my detect_file_type, which evaluates the file contents and determines what image type it is:



fn detect_file_type(data : &Vec<u8>) -> Option<String> 
if data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 // ?PNG
Some("png".to_string())
else if data[6] == 0x4A && data[7] == 0x46 && data[8] == 0x49 && data[9] == 0x46 // JFIF
Some("jpg".to_string())
else if data[6] == 0x45 && data[7] == 0x78 && data[8] == 0x69 && data[9] == 0x66 // Exif
Some("jpg".to_string())
else
None




This is rather simple.



Next, I wanted my directory search function:



fn find_dir(search : &str, path : PathBuf) -> Option<PathBuf> 
for entry in fs::read_dir(path).unwrap()
let path = entry.unwrap().path();
if path.is_dir()
if path.file_name().unwrap().to_str().unwrap().starts_with(search)
return Some(path);



return None;



Again, rather simple. We stuck to Option with both of these so that we can handle the error cases (if we need to).



And finally, we have all the critical work:



fn main() 
let args =
App::new("Pull Windows Lock-Screen Pictures")
.version("0.1")
.about("Loads the images used for the main Windows 10 Lock-Screen backgrounds to the specified folder (or './out' if not specified).")
.arg(Arg::with_name("destination")
.help("The destination directory ('./out' by default)")
.takes_value(true)
.required(false))
.get_matches();

let my_dirs = Directories::with_prefix("windows_lock_screen_pictures", "Windows_Lock_Screen_Pictures").unwrap();
let home = my_dirs.bin_home().parent().unwrap().parent().unwrap().join("Packages");
let dir = find_dir("Microsoft.Windows.ContentDeliveryManager", home).unwrap().join("LocalState").join("Assets");

for entry in fs::read_dir(dir).unwrap()
let path = entry.unwrap().path();
if !path.is_dir()
let data = fs::read(&path).unwrap();
let path_str = path.display().to_string();
let file = path.file_name().unwrap().to_str().unwrap().to_string();

let path_ext =
match detect_file_type(&data)
Some(path_ext) =>
let mut res = ".".to_string();
res.push_str(&path_ext);
res
,
_ => "".to_string()
;

let mut base_dest_dir = "".to_string();
let mut default = std::env::current_dir().unwrap().to_str().unwrap().to_string();
default.push_str("\out\");
let dest_dir = args.value_of("destination").unwrap_or(&default);
base_dest_dir.push_str(dest_dir);

if !Path::new(&base_dest_dir).exists()
fs::create_dir(Path::new(&base_dest_dir)).expect("Could not create directory");


base_dest_dir.push_str(&file);
base_dest_dir.push_str(&path_ext);

println!(" -> ", path_str, base_dest_dir);
fs::write(Path::new(&base_dest_dir), data).expect("Could not write file");





Overall, we kept things rather small, but still allowed flexibility (and most of the safety we needed).



Our whole program is as follows:



extern crate clap;
extern crate dirs;



use std::fs::self;
use std::path::Path,PathBuf;
use clap::App,Arg;
use dirs::Directories;

fn detect_file_type(data : &Vec<u8>) -> Option<String>
if data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 // ?PNG
Some("png".to_string())
else if data[6] == 0x4A && data[7] == 0x46 && data[8] == 0x49 && data[9] == 0x46 // JFIF
Some("jpg".to_string())
else if data[6] == 0x45 && data[7] == 0x78 && data[8] == 0x69 && data[9] == 0x66 // Exif
Some("jpg".to_string())
else
None



fn find_dir(search : &str, path : PathBuf) -> Option<PathBuf>
for entry in fs::read_dir(path).unwrap()
let path = entry.unwrap().path();
if path.is_dir()
if path.file_name().unwrap().to_str().unwrap().starts_with(search)
return Some(path);



return None;


fn main()
let args =
App::new("Pull Windows Lock-Screen Pictures")
.version("0.1")
.about("Loads the images used for the main Windows 10 Lock-Screen backgrounds to the specified folder (or './out' if not specified).")
.arg(Arg::with_name("destination")
.help("The destination directory ('./out' by default)")
.takes_value(true)
.required(false))
.get_matches();

let my_dirs = Directories::with_prefix("windows_lock_screen_pictures", "Windows_Lock_Screen_Pictures").unwrap();
let home = my_dirs.bin_home().parent().unwrap().parent().unwrap().join("Packages");
let dir = find_dir("Microsoft.Windows.ContentDeliveryManager", home).unwrap().join("LocalState").join("Assets");

for entry in fs::read_dir(dir).unwrap()
let path = entry.unwrap().path();
if !path.is_dir()
let data = fs::read(&path).unwrap();
let path_str = path.display().to_string();
let file = path.file_name().unwrap().to_str().unwrap().to_string();

let path_ext =
match detect_file_type(&data)
Some(path_ext) =>
let mut res = ".".to_string();
res.push_str(&path_ext);
res
,
_ => "".to_string()
;

let mut base_dest_dir = "".to_string();
let mut default = std::env::current_dir().unwrap().to_str().unwrap().to_string();
default.push_str("\out\");
let dest_dir = args.value_of("destination").unwrap_or(&default);
base_dest_dir.push_str(dest_dir);

if !Path::new(&base_dest_dir).exists()
fs::create_dir(Path::new(&base_dest_dir)).expect("Could not create directory");


base_dest_dir.push_str(&file);
base_dest_dir.push_str(&path_ext);

println!(" -> ", path_str, base_dest_dir);
fs::write(Path::new(&base_dest_dir), data).expect("Could not write file");











share









$endgroup$


















    0












    $begingroup$


    Windows 10 has an interesting feature where it will display "fun facts" and images on the lock screen. Occasionally, these images are something I would want to use for a background.



    These images are stored in %LocalAppData%PackagesMicrosoft.Windows.ContentDeliveryManager_cw5n1h2txyewyLocalStateAssets.



    As I wasn't certain if _cw5n1h2txyewy would be constant on the Microsoft.Windows.ContentDeliveryManager folder, I wanted to just search the %LocalAppData%Packages directory for Microsoft.Windows.ContentDeliveryManager*, and pull the first directory that matches that pattern.



    Then, in that directory, I wanted to copy the images and read a "magic number" from each image, which can vary but determines the image type. In this case, I use the first 4 bytes as 0x89 0x50 0x4E 0x47 (or 0x89 then PNG), or bytes 6 through 9 as 0x4A 0x46 0x49 0x46 (JFIF) or 0x45 0x78 0x69 0x66 (Exif). The first condition would add the png extension, the second would add jpg.



    The first function is my detect_file_type, which evaluates the file contents and determines what image type it is:



    fn detect_file_type(data : &Vec<u8>) -> Option<String> 
    if data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 // ?PNG
    Some("png".to_string())
    else if data[6] == 0x4A && data[7] == 0x46 && data[8] == 0x49 && data[9] == 0x46 // JFIF
    Some("jpg".to_string())
    else if data[6] == 0x45 && data[7] == 0x78 && data[8] == 0x69 && data[9] == 0x66 // Exif
    Some("jpg".to_string())
    else
    None




    This is rather simple.



    Next, I wanted my directory search function:



    fn find_dir(search : &str, path : PathBuf) -> Option<PathBuf> 
    for entry in fs::read_dir(path).unwrap()
    let path = entry.unwrap().path();
    if path.is_dir()
    if path.file_name().unwrap().to_str().unwrap().starts_with(search)
    return Some(path);



    return None;



    Again, rather simple. We stuck to Option with both of these so that we can handle the error cases (if we need to).



    And finally, we have all the critical work:



    fn main() 
    let args =
    App::new("Pull Windows Lock-Screen Pictures")
    .version("0.1")
    .about("Loads the images used for the main Windows 10 Lock-Screen backgrounds to the specified folder (or './out' if not specified).")
    .arg(Arg::with_name("destination")
    .help("The destination directory ('./out' by default)")
    .takes_value(true)
    .required(false))
    .get_matches();

    let my_dirs = Directories::with_prefix("windows_lock_screen_pictures", "Windows_Lock_Screen_Pictures").unwrap();
    let home = my_dirs.bin_home().parent().unwrap().parent().unwrap().join("Packages");
    let dir = find_dir("Microsoft.Windows.ContentDeliveryManager", home).unwrap().join("LocalState").join("Assets");

    for entry in fs::read_dir(dir).unwrap()
    let path = entry.unwrap().path();
    if !path.is_dir()
    let data = fs::read(&path).unwrap();
    let path_str = path.display().to_string();
    let file = path.file_name().unwrap().to_str().unwrap().to_string();

    let path_ext =
    match detect_file_type(&data)
    Some(path_ext) =>
    let mut res = ".".to_string();
    res.push_str(&path_ext);
    res
    ,
    _ => "".to_string()
    ;

    let mut base_dest_dir = "".to_string();
    let mut default = std::env::current_dir().unwrap().to_str().unwrap().to_string();
    default.push_str("\out\");
    let dest_dir = args.value_of("destination").unwrap_or(&default);
    base_dest_dir.push_str(dest_dir);

    if !Path::new(&base_dest_dir).exists()
    fs::create_dir(Path::new(&base_dest_dir)).expect("Could not create directory");


    base_dest_dir.push_str(&file);
    base_dest_dir.push_str(&path_ext);

    println!(" -> ", path_str, base_dest_dir);
    fs::write(Path::new(&base_dest_dir), data).expect("Could not write file");





    Overall, we kept things rather small, but still allowed flexibility (and most of the safety we needed).



    Our whole program is as follows:



    extern crate clap;
    extern crate dirs;



    use std::fs::self;
    use std::path::Path,PathBuf;
    use clap::App,Arg;
    use dirs::Directories;

    fn detect_file_type(data : &Vec<u8>) -> Option<String>
    if data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 // ?PNG
    Some("png".to_string())
    else if data[6] == 0x4A && data[7] == 0x46 && data[8] == 0x49 && data[9] == 0x46 // JFIF
    Some("jpg".to_string())
    else if data[6] == 0x45 && data[7] == 0x78 && data[8] == 0x69 && data[9] == 0x66 // Exif
    Some("jpg".to_string())
    else
    None



    fn find_dir(search : &str, path : PathBuf) -> Option<PathBuf>
    for entry in fs::read_dir(path).unwrap()
    let path = entry.unwrap().path();
    if path.is_dir()
    if path.file_name().unwrap().to_str().unwrap().starts_with(search)
    return Some(path);



    return None;


    fn main()
    let args =
    App::new("Pull Windows Lock-Screen Pictures")
    .version("0.1")
    .about("Loads the images used for the main Windows 10 Lock-Screen backgrounds to the specified folder (or './out' if not specified).")
    .arg(Arg::with_name("destination")
    .help("The destination directory ('./out' by default)")
    .takes_value(true)
    .required(false))
    .get_matches();

    let my_dirs = Directories::with_prefix("windows_lock_screen_pictures", "Windows_Lock_Screen_Pictures").unwrap();
    let home = my_dirs.bin_home().parent().unwrap().parent().unwrap().join("Packages");
    let dir = find_dir("Microsoft.Windows.ContentDeliveryManager", home).unwrap().join("LocalState").join("Assets");

    for entry in fs::read_dir(dir).unwrap()
    let path = entry.unwrap().path();
    if !path.is_dir()
    let data = fs::read(&path).unwrap();
    let path_str = path.display().to_string();
    let file = path.file_name().unwrap().to_str().unwrap().to_string();

    let path_ext =
    match detect_file_type(&data)
    Some(path_ext) =>
    let mut res = ".".to_string();
    res.push_str(&path_ext);
    res
    ,
    _ => "".to_string()
    ;

    let mut base_dest_dir = "".to_string();
    let mut default = std::env::current_dir().unwrap().to_str().unwrap().to_string();
    default.push_str("\out\");
    let dest_dir = args.value_of("destination").unwrap_or(&default);
    base_dest_dir.push_str(dest_dir);

    if !Path::new(&base_dest_dir).exists()
    fs::create_dir(Path::new(&base_dest_dir)).expect("Could not create directory");


    base_dest_dir.push_str(&file);
    base_dest_dir.push_str(&path_ext);

    println!(" -> ", path_str, base_dest_dir);
    fs::write(Path::new(&base_dest_dir), data).expect("Could not write file");











    share









    $endgroup$














      0












      0








      0





      $begingroup$


      Windows 10 has an interesting feature where it will display "fun facts" and images on the lock screen. Occasionally, these images are something I would want to use for a background.



      These images are stored in %LocalAppData%PackagesMicrosoft.Windows.ContentDeliveryManager_cw5n1h2txyewyLocalStateAssets.



      As I wasn't certain if _cw5n1h2txyewy would be constant on the Microsoft.Windows.ContentDeliveryManager folder, I wanted to just search the %LocalAppData%Packages directory for Microsoft.Windows.ContentDeliveryManager*, and pull the first directory that matches that pattern.



      Then, in that directory, I wanted to copy the images and read a "magic number" from each image, which can vary but determines the image type. In this case, I use the first 4 bytes as 0x89 0x50 0x4E 0x47 (or 0x89 then PNG), or bytes 6 through 9 as 0x4A 0x46 0x49 0x46 (JFIF) or 0x45 0x78 0x69 0x66 (Exif). The first condition would add the png extension, the second would add jpg.



      The first function is my detect_file_type, which evaluates the file contents and determines what image type it is:



      fn detect_file_type(data : &Vec<u8>) -> Option<String> 
      if data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 // ?PNG
      Some("png".to_string())
      else if data[6] == 0x4A && data[7] == 0x46 && data[8] == 0x49 && data[9] == 0x46 // JFIF
      Some("jpg".to_string())
      else if data[6] == 0x45 && data[7] == 0x78 && data[8] == 0x69 && data[9] == 0x66 // Exif
      Some("jpg".to_string())
      else
      None




      This is rather simple.



      Next, I wanted my directory search function:



      fn find_dir(search : &str, path : PathBuf) -> Option<PathBuf> 
      for entry in fs::read_dir(path).unwrap()
      let path = entry.unwrap().path();
      if path.is_dir()
      if path.file_name().unwrap().to_str().unwrap().starts_with(search)
      return Some(path);



      return None;



      Again, rather simple. We stuck to Option with both of these so that we can handle the error cases (if we need to).



      And finally, we have all the critical work:



      fn main() 
      let args =
      App::new("Pull Windows Lock-Screen Pictures")
      .version("0.1")
      .about("Loads the images used for the main Windows 10 Lock-Screen backgrounds to the specified folder (or './out' if not specified).")
      .arg(Arg::with_name("destination")
      .help("The destination directory ('./out' by default)")
      .takes_value(true)
      .required(false))
      .get_matches();

      let my_dirs = Directories::with_prefix("windows_lock_screen_pictures", "Windows_Lock_Screen_Pictures").unwrap();
      let home = my_dirs.bin_home().parent().unwrap().parent().unwrap().join("Packages");
      let dir = find_dir("Microsoft.Windows.ContentDeliveryManager", home).unwrap().join("LocalState").join("Assets");

      for entry in fs::read_dir(dir).unwrap()
      let path = entry.unwrap().path();
      if !path.is_dir()
      let data = fs::read(&path).unwrap();
      let path_str = path.display().to_string();
      let file = path.file_name().unwrap().to_str().unwrap().to_string();

      let path_ext =
      match detect_file_type(&data)
      Some(path_ext) =>
      let mut res = ".".to_string();
      res.push_str(&path_ext);
      res
      ,
      _ => "".to_string()
      ;

      let mut base_dest_dir = "".to_string();
      let mut default = std::env::current_dir().unwrap().to_str().unwrap().to_string();
      default.push_str("\out\");
      let dest_dir = args.value_of("destination").unwrap_or(&default);
      base_dest_dir.push_str(dest_dir);

      if !Path::new(&base_dest_dir).exists()
      fs::create_dir(Path::new(&base_dest_dir)).expect("Could not create directory");


      base_dest_dir.push_str(&file);
      base_dest_dir.push_str(&path_ext);

      println!(" -> ", path_str, base_dest_dir);
      fs::write(Path::new(&base_dest_dir), data).expect("Could not write file");





      Overall, we kept things rather small, but still allowed flexibility (and most of the safety we needed).



      Our whole program is as follows:



      extern crate clap;
      extern crate dirs;



      use std::fs::self;
      use std::path::Path,PathBuf;
      use clap::App,Arg;
      use dirs::Directories;

      fn detect_file_type(data : &Vec<u8>) -> Option<String>
      if data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 // ?PNG
      Some("png".to_string())
      else if data[6] == 0x4A && data[7] == 0x46 && data[8] == 0x49 && data[9] == 0x46 // JFIF
      Some("jpg".to_string())
      else if data[6] == 0x45 && data[7] == 0x78 && data[8] == 0x69 && data[9] == 0x66 // Exif
      Some("jpg".to_string())
      else
      None



      fn find_dir(search : &str, path : PathBuf) -> Option<PathBuf>
      for entry in fs::read_dir(path).unwrap()
      let path = entry.unwrap().path();
      if path.is_dir()
      if path.file_name().unwrap().to_str().unwrap().starts_with(search)
      return Some(path);



      return None;


      fn main()
      let args =
      App::new("Pull Windows Lock-Screen Pictures")
      .version("0.1")
      .about("Loads the images used for the main Windows 10 Lock-Screen backgrounds to the specified folder (or './out' if not specified).")
      .arg(Arg::with_name("destination")
      .help("The destination directory ('./out' by default)")
      .takes_value(true)
      .required(false))
      .get_matches();

      let my_dirs = Directories::with_prefix("windows_lock_screen_pictures", "Windows_Lock_Screen_Pictures").unwrap();
      let home = my_dirs.bin_home().parent().unwrap().parent().unwrap().join("Packages");
      let dir = find_dir("Microsoft.Windows.ContentDeliveryManager", home).unwrap().join("LocalState").join("Assets");

      for entry in fs::read_dir(dir).unwrap()
      let path = entry.unwrap().path();
      if !path.is_dir()
      let data = fs::read(&path).unwrap();
      let path_str = path.display().to_string();
      let file = path.file_name().unwrap().to_str().unwrap().to_string();

      let path_ext =
      match detect_file_type(&data)
      Some(path_ext) =>
      let mut res = ".".to_string();
      res.push_str(&path_ext);
      res
      ,
      _ => "".to_string()
      ;

      let mut base_dest_dir = "".to_string();
      let mut default = std::env::current_dir().unwrap().to_str().unwrap().to_string();
      default.push_str("\out\");
      let dest_dir = args.value_of("destination").unwrap_or(&default);
      base_dest_dir.push_str(dest_dir);

      if !Path::new(&base_dest_dir).exists()
      fs::create_dir(Path::new(&base_dest_dir)).expect("Could not create directory");


      base_dest_dir.push_str(&file);
      base_dest_dir.push_str(&path_ext);

      println!(" -> ", path_str, base_dest_dir);
      fs::write(Path::new(&base_dest_dir), data).expect("Could not write file");











      share









      $endgroup$




      Windows 10 has an interesting feature where it will display "fun facts" and images on the lock screen. Occasionally, these images are something I would want to use for a background.



      These images are stored in %LocalAppData%PackagesMicrosoft.Windows.ContentDeliveryManager_cw5n1h2txyewyLocalStateAssets.



      As I wasn't certain if _cw5n1h2txyewy would be constant on the Microsoft.Windows.ContentDeliveryManager folder, I wanted to just search the %LocalAppData%Packages directory for Microsoft.Windows.ContentDeliveryManager*, and pull the first directory that matches that pattern.



      Then, in that directory, I wanted to copy the images and read a "magic number" from each image, which can vary but determines the image type. In this case, I use the first 4 bytes as 0x89 0x50 0x4E 0x47 (or 0x89 then PNG), or bytes 6 through 9 as 0x4A 0x46 0x49 0x46 (JFIF) or 0x45 0x78 0x69 0x66 (Exif). The first condition would add the png extension, the second would add jpg.



      The first function is my detect_file_type, which evaluates the file contents and determines what image type it is:



      fn detect_file_type(data : &Vec<u8>) -> Option<String> 
      if data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 // ?PNG
      Some("png".to_string())
      else if data[6] == 0x4A && data[7] == 0x46 && data[8] == 0x49 && data[9] == 0x46 // JFIF
      Some("jpg".to_string())
      else if data[6] == 0x45 && data[7] == 0x78 && data[8] == 0x69 && data[9] == 0x66 // Exif
      Some("jpg".to_string())
      else
      None




      This is rather simple.



      Next, I wanted my directory search function:



      fn find_dir(search : &str, path : PathBuf) -> Option<PathBuf> 
      for entry in fs::read_dir(path).unwrap()
      let path = entry.unwrap().path();
      if path.is_dir()
      if path.file_name().unwrap().to_str().unwrap().starts_with(search)
      return Some(path);



      return None;



      Again, rather simple. We stuck to Option with both of these so that we can handle the error cases (if we need to).



      And finally, we have all the critical work:



      fn main() 
      let args =
      App::new("Pull Windows Lock-Screen Pictures")
      .version("0.1")
      .about("Loads the images used for the main Windows 10 Lock-Screen backgrounds to the specified folder (or './out' if not specified).")
      .arg(Arg::with_name("destination")
      .help("The destination directory ('./out' by default)")
      .takes_value(true)
      .required(false))
      .get_matches();

      let my_dirs = Directories::with_prefix("windows_lock_screen_pictures", "Windows_Lock_Screen_Pictures").unwrap();
      let home = my_dirs.bin_home().parent().unwrap().parent().unwrap().join("Packages");
      let dir = find_dir("Microsoft.Windows.ContentDeliveryManager", home).unwrap().join("LocalState").join("Assets");

      for entry in fs::read_dir(dir).unwrap()
      let path = entry.unwrap().path();
      if !path.is_dir()
      let data = fs::read(&path).unwrap();
      let path_str = path.display().to_string();
      let file = path.file_name().unwrap().to_str().unwrap().to_string();

      let path_ext =
      match detect_file_type(&data)
      Some(path_ext) =>
      let mut res = ".".to_string();
      res.push_str(&path_ext);
      res
      ,
      _ => "".to_string()
      ;

      let mut base_dest_dir = "".to_string();
      let mut default = std::env::current_dir().unwrap().to_str().unwrap().to_string();
      default.push_str("\out\");
      let dest_dir = args.value_of("destination").unwrap_or(&default);
      base_dest_dir.push_str(dest_dir);

      if !Path::new(&base_dest_dir).exists()
      fs::create_dir(Path::new(&base_dest_dir)).expect("Could not create directory");


      base_dest_dir.push_str(&file);
      base_dest_dir.push_str(&path_ext);

      println!(" -> ", path_str, base_dest_dir);
      fs::write(Path::new(&base_dest_dir), data).expect("Could not write file");





      Overall, we kept things rather small, but still allowed flexibility (and most of the safety we needed).



      Our whole program is as follows:



      extern crate clap;
      extern crate dirs;



      use std::fs::self;
      use std::path::Path,PathBuf;
      use clap::App,Arg;
      use dirs::Directories;

      fn detect_file_type(data : &Vec<u8>) -> Option<String>
      if data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 // ?PNG
      Some("png".to_string())
      else if data[6] == 0x4A && data[7] == 0x46 && data[8] == 0x49 && data[9] == 0x46 // JFIF
      Some("jpg".to_string())
      else if data[6] == 0x45 && data[7] == 0x78 && data[8] == 0x69 && data[9] == 0x66 // Exif
      Some("jpg".to_string())
      else
      None



      fn find_dir(search : &str, path : PathBuf) -> Option<PathBuf>
      for entry in fs::read_dir(path).unwrap()
      let path = entry.unwrap().path();
      if path.is_dir()
      if path.file_name().unwrap().to_str().unwrap().starts_with(search)
      return Some(path);



      return None;


      fn main()
      let args =
      App::new("Pull Windows Lock-Screen Pictures")
      .version("0.1")
      .about("Loads the images used for the main Windows 10 Lock-Screen backgrounds to the specified folder (or './out' if not specified).")
      .arg(Arg::with_name("destination")
      .help("The destination directory ('./out' by default)")
      .takes_value(true)
      .required(false))
      .get_matches();

      let my_dirs = Directories::with_prefix("windows_lock_screen_pictures", "Windows_Lock_Screen_Pictures").unwrap();
      let home = my_dirs.bin_home().parent().unwrap().parent().unwrap().join("Packages");
      let dir = find_dir("Microsoft.Windows.ContentDeliveryManager", home).unwrap().join("LocalState").join("Assets");

      for entry in fs::read_dir(dir).unwrap()
      let path = entry.unwrap().path();
      if !path.is_dir()
      let data = fs::read(&path).unwrap();
      let path_str = path.display().to_string();
      let file = path.file_name().unwrap().to_str().unwrap().to_string();

      let path_ext =
      match detect_file_type(&data)
      Some(path_ext) =>
      let mut res = ".".to_string();
      res.push_str(&path_ext);
      res
      ,
      _ => "".to_string()
      ;

      let mut base_dest_dir = "".to_string();
      let mut default = std::env::current_dir().unwrap().to_str().unwrap().to_string();
      default.push_str("\out\");
      let dest_dir = args.value_of("destination").unwrap_or(&default);
      base_dest_dir.push_str(dest_dir);

      if !Path::new(&base_dest_dir).exists()
      fs::create_dir(Path::new(&base_dest_dir)).expect("Could not create directory");


      base_dest_dir.push_str(&file);
      base_dest_dir.push_str(&path_ext);

      println!(" -> ", path_str, base_dest_dir);
      fs::write(Path::new(&base_dest_dir), data).expect("Could not write file");









      file rust





      share












      share










      share



      share










      asked 3 mins ago









      Der KommissarDer Kommissar

      15.8k251136




      15.8k251136




















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217745%2fpulling-images-from-windows-10-lock-screen-to-a-different-folder%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217745%2fpulling-images-from-windows-10-lock-screen-to-a-different-folder%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          格濟夫卡 參考資料 导航菜单51°3′40″N 34°2′21″E / 51.06111°N 34.03917°E / 51.06111; 34.03917ГезівкаПогода в селі 编辑或修订

          聖斯德望教堂 (塞克什白堡) 參考資料 导航菜单