Sunday, August 4, 2019

java - When a map variable is passed to constructor of different instances, all instances member variables are updated to latest value of map




Main Class --



package test;
import java.util.Map;

public class Client {
private static ArrayList allInstances = new ArrayList();
private static Map var1 = new HashMap();


public static void main(String[] args)
{
var1.put("key1","value1");
Class1 instance1 = new Class1(var1);
allInstances.add(instance1);

var1.put("key2","value2");
Class1 instance2 = new Class1(var1);
allInstances.add(instance2);


getInstances();
}

public static void getInstances() {
for(Class1 c: allInstances) {
System.out.println(c.getClassDetails());
}
}



Class Class1 --



package test
import java.util.Map;

public class Class1 {
private Map classDetails;

public Class1(Map classDetails) {

this.classDetails = classDetails;
}

public Map getClassDetails(){
return this.classDetails;
}
}


Output--




{key2=value2}
{key2=value2}


As we can see from the output above, both instances variable returns the same updated value. Should'nt instance1 return {key1=value1}



Also, if this is the expected behavior, what can be done to tackle this issue.


Answer



As it is appeared from your code, you referenced same HashMap to instacne1 and instance2 objects and in getClassDetails method the tostring method of same hashmap will invoked so the outputs is the same , use this code snippet :




import java.util.*;

public class Main {
private static ArrayList allInstances = new ArrayList();

public static void main(String[] args)
{
Map var = new HashMap();
var.put("key1","value1");

Class1 instance1 = new Class1(var);
allInstances.add(instance1);

var = new HashMap();
var.put("key2","value2");
Class1 instance2 = new Class1(var);
allInstances.add(instance2);

getInstances();
}


public static void getInstances() {
for(Class1 c: allInstances)
System.out.println(c.getClassDetails());
}
}

batch - File last modification time with seconds


I want to know when a file has been modified for the last time.


I can get these infos using the following batch script:



FOR %%i IN (myfile) DO SET modif_time=%%~ti



The problem is that I need the second of the last modification and the command %~t returns the date and the time with only hours and minutes.


I can only check the seconds by manually viewing the "property window" file by file.


How can I get the time with seconds in batch?


Answer



Windows Server 2003 and later


With a little effort you can use forfiles to get the last modified time of a specific file, seconds included:


REM "delims=" is required to avoid stripping AM/PM
for /f "delims=" %%i in ('"forfiles /m filename /c "cmd /c echo @ftime" "') do set modif_time=%%i
echo %modif_time%

Example output


7:33:54 AM

The value displayed is based on the local time of the computer and matches the time shown in the file properties dialog.


Usage help


http://technet.microsoft.com/en-us/library/cc753551.aspx



forfiles.exe is not available out of the box, however you can manually get the required executable. It's an old version which is part of the Windows 2000 Resource Kit. The syntax is case-sensitive and slightly different, and so is the output:


for /f %%i in ('"forfiles.exe -mfilename -c"cmd /c echo @FTIME" "') do set modif_time=%%i
echo %modif_time%

Example output


153354

Here the time value is displayed in the UTC format and is not affected by changes in time zone or daylight saving time. In this example the file was last modified at 15:33:54 (UTC).


Note You can obtain the newer forfiles.exe version by grabbing a copy of the file from any Windows 2003 Server installation or setup media.


Linked Excel 2010 Charts Won't Show Field Codes in Word 2010 - What am I doing wrong?


Thank you for reading. I hope this is something simple I am overlooking.


Synopsis: I have a Word doc containing linked charts from a single Excel source. They are docx and xlsx files created in Word and Excel 2010 versions on a PC. I created the charts in Excel, copied and pasted into Word and selected "Keep Source Formatting and Link to Excel" from the clipboard popup. I wish to be able to switch the linked source file for all charts easily using Find and Replace on the Field Codes (feel free to suggest a better way).


Issue: When I try to display the Field Codes for the charts using ALT+F9 or View Field Codes button ({a}), nothing happens to the charts. No Field Codes appear, they just stay charts. I can see other field codes within the document but not for the charts.


I have tried: Using ALT+F9, using the View Field Codes button, checking and un-checking "Show field codes instead of their values" Under File > Options > Advanced.


I appreciate any clues!


Answer



Okay, after much research, I finally just tried a different way of pasting the charts in. Instead of pasting using CTRL+V and using the clipboard popup to select "Keep Source Formatting and Link to Excel", I had to use the Home > Paste... Paste Special... menu option (or ALT+CTRL+V), then choose Paste link: Microsoft Excel Chart Object. I can toggle the field code visibility just fine for objects pasted in this way.


The pasted object seems to behave differently in Word, however. It re-sizes like a picture instead of a chart (stretches text). And the Chart Tools ribbon is not available when it is selected, but it will work for my needs.


Sorry I ended up (sort of) answering myself. Maybe this will help someone else. Thanks!


Saturday, August 3, 2019

windows - How to batch copy & rename files?



I'm using windows server 2012 R2. I have a folder with a bunch of files and I want to copy for every file in that folder 20 times into another folder but the newly copied file has to be renamed using single alphabetical orders. For example a file called "orange.html" gets copied 20 times and moved to another folder. The new folder would contain 20 new copied files with file names such as a.html, b.html, c.html etc.



This is the code but all it does increment by numbers but I want to increment by the alphabet



@echo off

for /L %%i IN (1,1,100) do call :docopy %%i

goto end

:docopy
set FN=00%1
set FN=%FN:~-3%

copy source-file.html poll%FN%.html

:end


Answer



All it does increment by numbers but I want to increment by the alphabet



The following batch file (test.cmd) should get you started:



@echo off
setlocal enableDelayedExpansion
set "chars=abcedefhijklmnopqrstuvwxyz"
for /l %%i in (0,1,25) do (
echo copy source-file.html folder\poll!chars:~%%i,1!.html

)
endlocal


Notes:




  • This is a partial answer because your requirements are not clear.

  • Use the above batch file as a starting point

  • It shows how to construct the file names using incremental letters of the alphabet.




Example output:



copy source-file.html folder\polla.html
copy source-file.html folder\pollb.html
copy source-file.html folder\pollc.html
copy source-file.html folder\polle.html
copy source-file.html folder\polld.html
copy source-file.html folder\polle.html

copy source-file.html folder\pollf.html
copy source-file.html folder\pollh.html
copy source-file.html folder\polli.html
copy source-file.html folder\pollj.html
copy source-file.html folder\pollk.html
copy source-file.html folder\polll.html
copy source-file.html folder\pollm.html
copy source-file.html folder\polln.html
copy source-file.html folder\pollo.html
copy source-file.html folder\pollp.html

copy source-file.html folder\pollq.html
copy source-file.html folder\pollr.html
copy source-file.html folder\polls.html
copy source-file.html folder\pollt.html
copy source-file.html folder\pollu.html
copy source-file.html folder\pollv.html
copy source-file.html folder\pollw.html
copy source-file.html folder\pollx.html
copy source-file.html folder\polly.html
copy source-file.html folder\pollz.html






Further Reading




  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.

  • enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.

  • for /l - Conditionally perform a command for a range of numbers.


  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.

  • variables - Extract part of a variable (substring).


windows xp - "Acces denied " message in Services just showed up



I have Windows XP SP3 and Avast antivirus on my notebook. So problem is that from yesterday I started to see message " Access denied" when I try to do anything with any service (Disable, Manual, Automatic).




I've done System restore a week a go, but 2 or 3 days ago the issues has occurred again. Should I make System Restore again? What could possibly be a reason?


Answer



After seeing other forums with similar issues it appears as if the error could be due to a conflict with a piece of software. One forum has mentioned it was due to a security update for their printer!



Disable ZoneAlarm or any firewall/AV you have and see if the error persists.



Open up MSCONFIG and, if you know what you are doing (if not, read up on it first) disable as much as you can from start up. If the problem goes then you'll have to add them one at a time until you find the culprit!



A Windows Update security update or similar could cause the issue. Roll back the system (System restore) and only download the updates you need.


Table column has non-standard date format, but Excel won't recognise date for that format

Problem
We have an Excel table where the column's category is date and the type has been set to "dd.m.yy"



enter image description here




Excel would recognise any new dates typed into the table of that format as dates.



At some point this stopped happening and now dates must be typed in, in the format "dd/mm/yy" at which point, Excel then formats them as "dd.mm.yy".



Why will Excel no longer recognise my date formats?



Set-up





  • Excel Version: Microsoft Office Professional Plus 2010

  • Windows: 10

  • File type: .xlsx

windows 10 - Fix permissions of C:Program FilesWindowsApps

Ok, so I was trying to edit files in the the WindowsApps directory (I made this post Remove pre-installed apps (Candy Crush, Bubble Witch Saga, March of Empires) from Windows 10) and I set myself as the owner of all of the folders in this directory. Then, this made Windows Store unlaunchable, so I began fiddling with the permissions, which was a big mistake. If I add a permission rule giving "All application packages" full control of the WindowsApps folder and subfolders, then Windows Store begins functioning again, but this seems like a band-aid fix so I tried to find another solution. I tried resetting Windows twice (once keeping files and once losing files) but this issue still persists.


I am running 64-bit Windows 10 Pro - I am preparing to reinstall Windows with a USB drive created through the media creation tool but I am worried this will install a standard version of Windows. Will I be able to keep the Pro version?


Does anyone have any other ideas - maybe ways to reset permissions of an entire folder tree or is there a way to delete the entire WindowsApps directory and have Window update repair it?


At this point, I am considering just using my computer as-is, because I don't really use the Windows store.. please help!

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...